package com.stylefeng.guns.modular.system.controller;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.github.qcloudsms.SmsSingleSenderResult;
import com.github.qcloudsms.SmsVoiceVerifyCodeSenderResult;
import com.stylefeng.guns.common.annotion.BussinessLog;
import com.stylefeng.guns.common.constant.Const;
import com.stylefeng.guns.common.constant.dictmap.OrderDict;
import com.stylefeng.guns.common.constant.factory.PageFactory;
import com.stylefeng.guns.common.exception.BizExceptionEnum;
import com.stylefeng.guns.common.persistence.dao.*;
import com.stylefeng.guns.common.persistence.model.*;
import com.stylefeng.guns.common.sms.sdk.YunpianClient;
import com.stylefeng.guns.common.sms.sdk.YunpianConf;
import com.stylefeng.guns.common.sms.sdk.model.Result;
import com.stylefeng.guns.common.sms.sdk.model.SmsSingleSend;
import com.stylefeng.guns.common.sms.sdk.model.VoiceSend;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.base.tips.ErrorTip;
import com.stylefeng.guns.core.datascope.DataScope;
import com.stylefeng.guns.core.log.LogObjectHolder;
import com.stylefeng.guns.core.page.PageInfoBT;
import com.stylefeng.guns.core.shiro.ShiroKit;
import com.stylefeng.guns.core.support.HttpKit;
import com.stylefeng.guns.core.util.DateUtil;
import com.stylefeng.guns.core.util.TencentSmsUtil;
import com.stylefeng.guns.modular.system.business.systemenum.OrderEnum;
import com.stylefeng.guns.modular.system.service.*;
import com.stylefeng.guns.modular.system.warpper.OrderWarpper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 订单管理控制器
 *
 * @author fengshuonan
 * @Date 2018-01-20 14:17:13
 */
@Controller
@RequestMapping("/tOrder")
public class TOrderController extends BaseController {

    private String PREFIX = "/system/tOrder/";
    @Autowired
    private ITOrderService tOrderService;
    @Autowired
    private ITOrderFeeService itOrderFeeService;
    @Autowired
    private TMonthbalanceDetailMapper monthbalanceDetailMapper;

    @Autowired
    private ITCooperationDotService tCooperationDotService;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private TNoticeMapper noticeMapper;
    @Autowired
    private DeptMapper deptMapper;
    @Autowired
    TTrainMapper tTrainMapper;
    @Autowired
    private ITTrainService tTrainService;
    @Resource
    private IRegionService regionService;
    @Autowired
    private DotMapper dotMapper;
    @Autowired
    TOrderMapper orderMapper;
    @Autowired
    TBillMapper billMapper;
    @Autowired
    OrderBackRecordMapper backRecordMapper;
    @Autowired
    TOrderFeeLogMapper orderFeeLogMapper;
    @Autowired
    private TOrderFeeMapper orderFeeWarpper;
    @Autowired
    private IRegionService iRegionService;
    @Autowired
    private DictMapper tDictMapper;


    /**
     * 跳转到订单管理首页
     */
    @RequestMapping("")
    public String index(String flag, Model model) {

        model.addAttribute("flag", flag);
        String title = "";
        if (flag.equals("0")) {
            title = "自提单据审核";
        } else if (flag.equals("1")) {
            title = "配送单据审核";
        } else {
            title = "中转单据审核";
        }
        model.addAttribute("title", title);
        return PREFIX + "tOrder.html";
    }

    @RequestMapping("/indexTransfer")
    public String indexTransfer(Model model) {
        return PREFIX + "transfer/transferOut_list.html";
    }

    /**
     * 配送出库
     *
     * @return
     */
    @RequestMapping("distributionList")
    public String distributionList() {
        return PREFIX + "orderDistribution/distributionOut_list.html";
    }


    /**
     * 自提出库
     */
    @RequestMapping("outIndex")
    public String outIndex() {
        return PREFIX + "tOrder_out.html";
    }

    /**
     * 自提批量出库
     */
    @RequestMapping("batchOutList")
    public String batchOutList() {
        return PREFIX + "batchOutList.html";
    }

    /**
     * 跳转到订单配送出库管理首页
     */
    @RequestMapping("outDisIndex")
    public String outDisIndex() {
        return PREFIX + "orderDistribution/tOrderDistribution.html";
    }

    /**
     * 跳转到添加订单管理
     */
    @RequestMapping("/tOrder_add")
    public String tOrderAdd() {
        return PREFIX + "tOrder_add.html";
    }
    /**
     * 自提单新增子单(配送单)  页面
     */
    @RequestMapping("/addSubDeliveryPage")
    public String addSubDeliveryPage(String id,Model model) {
        model.addAttribute("id",id);
        TOrder torder=tOrderService.selectById(id);
        EntityWrapper<TOrder> ew= new EntityWrapper<TOrder>();
        ew.like("original_num",torder.getOriginal_num());
        int count=tOrderService.selectCount(ew);
        model.addAttribute("original_num_sub",torder.getOriginal_num()+"_0"+count);
        return PREFIX + "addSubDelivery.html";
    }
    /**
     * 跳转到修改订单管理
     */
    @RequestMapping("/tOrder_update/{tOrderId}")
    public String tOrderUpdate(@PathVariable Integer tOrderId, Model model) {
        TOrder tOrder = tOrderService.selectById(tOrderId);

        List<Region> regionList = regionService.selectListByPid("1");//查找中国下级区域，即省份信息
        model.addAttribute("regionList", regionList);

        List<Region> city = regionService.selectListByPid(tOrder.getProvince_code() + "");
        model.addAttribute("cityList", city);
        List<Region> reg = regionService.selectListByPid(tOrder.getCity_code() + "");
        model.addAttribute("regList", reg);

        model.addAttribute("item", tOrder);
        LogObjectHolder.me().set(tOrder);
        return PREFIX + "tOrder_edit.html";
    }

    /**
     * 跳转到修改订单费用
     */
    @RequestMapping("/the_library/{tOrderId}")
    public String theLibrary(@PathVariable Integer tOrderId, Model model) {
        TOrder tOrder = tOrderService.selectById(tOrderId);
        Date curDate = new Date();
        long time = curDate.getTime() - tOrder.getCreatetime().getTime();
        long days = time / (1000 * 60 * 60 * 24);
        List<Map<String, Object>> feeOrders = tOrderService.getOrderFee(tOrderId + "");
        model.addAttribute("item", tOrder);
        model.addAttribute("days", days);
        model.addAttribute("inTime", DateUtil.formatDate(tOrder.getArrive_date(), ""));
        model.addAttribute("feeOrders", feeOrders);
        List<Map<String, Object>> list = tOrderService.getFee(ShiroKit.getUser().getDeptId() + "");
        model.addAttribute("list", list);
        model.addAttribute("title", tOrder.getMode() == 0 ? "自提" : "配送");
        LogObjectHolder.me().set(tOrder);
        return PREFIX + "the_library.html";
    }


    @RequestMapping("/hxLibrary/{tOrderId}")
    public String hxLibrary(@PathVariable Integer tOrderId, Model model) {
        TOrder tOrder = tOrderService.selectById(tOrderId);
        Date curDate = new Date();
        long time = curDate.getTime() - tOrder.getCreatetime().getTime();
        long days = time / (1000 * 60 * 60 * 24);
        List<Map<String, Object>> feeOrders = tOrderService.getOrderFee(tOrderId + "");
        model.addAttribute("item", tOrder);
        model.addAttribute("days", days);
        model.addAttribute("inTime", DateUtil.formatDate(tOrder.getArrive_date(), ""));
        model.addAttribute("feeOrders", feeOrders);
        List<Map<String, Object>> list = tOrderService.getFee(ShiroKit.getUser().getDeptId() + "");
        model.addAttribute("list", list);
        model.addAttribute("title", tOrder.getMode() == 0 ? "自提" : "配送");

        List<Map> bills = billMapper.billMap(tOrder.getId() + "");
        model.addAttribute("bills", bills);
        LogObjectHolder.me().set(tOrder);
        return PREFIX + "hxLibrary.html";
    }


    @RequestMapping("/superlibrary/{tOrderId}")
    public String superlibrary(@PathVariable Integer tOrderId, Model model) {
        TOrder tOrder = tOrderService.selectById(tOrderId);
        Date curDate = new Date();
        long time = curDate.getTime() - tOrder.getCreatetime().getTime();
        long days = time / (1000 * 60 * 60 * 24);
        List<Map<String, Object>> feeOrders = tOrderService.getOrderFee(tOrderId + "");
        model.addAttribute("item", tOrder);
        model.addAttribute("days", days);
        model.addAttribute("feeOrders", feeOrders);
        List<Map<String, Object>> list = tOrderService.getFee(ShiroKit.getUser().getDeptId() + "");
        model.addAttribute("list", list);
        model.addAttribute("title", tOrder.getMode() == 0 ? "自提" : "配送");
        LogObjectHolder.me().set(tOrder);
        return PREFIX + "superOrder/the_library.html";
    }


    /**
     * 获取网点管理列表
     */
    @RequestMapping(value = "/searchList")
    @ResponseBody
    public Object searchList() {
        DataScope dataScope = new DataScope(ShiroKit.getDeptDataScope());
        List<Map<String, Object>> maps = tOrderService.selectDriverList(ShiroKit.getUser().deptId + "");
        Map map = new HashMap();
        map.put("message", "");
        map.put("code", "200");
        map.put("redirect", "");
        map.put("value", maps);
        return map;
    }

    @RequestMapping(value = "/searchDotList")
    @ResponseBody
    public Object searchDotList() {

        Page<Map<String, Object>> page=new Page(0,1000);
        PageInfoBT pageInfoBT=new PageInfoBT(page);
        Map m=new HashMap();
        m.put("deptid",ShiroKit.getUser().getDeptId());
        m.put("name","");
        List<Map<String, Object>> maps =tCooperationDotService.selectList(page,m);
        Map map = new HashMap();
        map.put("message", "");
        map.put("code", "200");
        map.put("redirect", "");
        map.put("value", maps);
        return map;
    }

    /**
     * 中转出库
     */
    @RequestMapping("/getTransferOrders")
    public String getTransferOrders(String ids, Model model) {
        model.addAttribute("ids", ids);
        return PREFIX + "transfer/transferOut.html";
    }

    /**
     * 自提单批量出库
     */
    @RequestMapping("/batchOutage")
    public String batchOutage(String ids, Model model) {
        model.addAttribute("ids", ids);
        return PREFIX + "batchOutage.html";
    }

    /**
     * 配送出库
     *
     * @return
     */
    @RequestMapping("/getDistributionOrders")
    public String getDistributionOrders(String ids, Model model) {
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < ids.split(",").length; i++) {
            list.add(Integer.valueOf(ids.split(",")[i]));
        }
        model.addAttribute("dotName", dotMapper.selectById(ShiroKit.getUser().getDotId()).getDotName());
        model.addAttribute("dotId", ShiroKit.getUser().getDotId());
        model.addAttribute("ids", ids);
        return PREFIX + "orderDistribution/distributionOut.html";
    }

    @RequestMapping("/printLib")
    public String printLib(String ids, Model model) {
        model.addAttribute("dotName", dotMapper.selectById(ShiroKit.getUser().getDotId()).getDotName());
        model.addAttribute("dotId", ShiroKit.getUser().getDotId());
        model.addAttribute("ids", ids);
        return PREFIX + "orderDistribution/printLib.html";
    }

    @RequestMapping("/printTransferLib")
    public String printTransferLib(String ids, Model model) {
        model.addAttribute("dotName", dotMapper.selectById(ShiroKit.getUser().getDotId()).getDotName());
        model.addAttribute("dotId", ShiroKit.getUser().getDotId());
        model.addAttribute("ids", ids);
        return PREFIX + "transfer/printTransferLib.html";
    }

    @RequestMapping(value = "/mDataList")
    @ResponseBody
    public Object mDataList(@RequestParam(required = false) String ids, Integer limit, Integer offset) {
        Page<Map<String, Object>> page = new Page(offset, limit);
        PageInfoBT pageInfoBT = new PageInfoBT(page);
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < ids.split(",").length; i++) {
            list.add(Integer.valueOf(ids.split(",")[i]));
        }
        List<Map<String, Object>> dots = tOrderService.selectDisOutList(page, list);
        pageInfoBT.setData(dots);
        pageInfoBT.setCount(page.getTotal());
        return pageInfoBT;
    }

    @RequestMapping(value = "/pDataList")
    @ResponseBody
    public Object pDataList(@RequestParam(required = false) String ids, Integer limit, Integer offset) {
        Page<Map<String, Object>> page = new Page(0, 1000);
        PageInfoBT pageInfoBT = new PageInfoBT(page);
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < ids.split(",").length; i++) {
            list.add(Integer.valueOf(ids.split(",")[i]));
        }
        List<Map<String, Object>> dots = tOrderService.selectDisOutList(page, list);
        pageInfoBT.setData(dots);
        pageInfoBT.setCount(page.getTotal());
        return pageInfoBT;
    }

    @RequestMapping(value = "/uDataList")
    @ResponseBody
    public Object uDataList(@RequestParam(required = false) String ids, Integer limit, Integer offset) {
        Page<Map<String, Object>> page = new Page(0, 1000);
        PageInfoBT pageInfoBT = new PageInfoBT(page);
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < ids.split(",").length; i++) {
            list.add(Integer.valueOf(ids.split(",")[i]));
        }
        List<Map<String, Object>> dots = tOrderService.selectZZOutList(page, list);
        pageInfoBT.setData(dots);
        pageInfoBT.setCount(page.getTotal());
        return pageInfoBT;
    }

    @RequestMapping(value = "/tDataList")
    @ResponseBody
    public Object tDataList(@RequestParam(required = false) String ids, Integer limit, Integer offset) {
        Page<Map<String, Object>> page = new Page(0, 10000);
        PageInfoBT pageInfoBT = new PageInfoBT(page);
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < ids.split(",").length; i++) {
            list.add(Integer.valueOf(ids.split(",")[i]));
        }
        List<Map<String, Object>> dots = tOrderService.selectZZOutList(page, list);
        pageInfoBT.setData(dots);
        pageInfoBT.setCount(page.getTotal());
        return pageInfoBT;
    }

    @RequestMapping(value = "/zzDataList")
    @ResponseBody
    public Object zzDataList(@RequestParam(required = false) String ids, Integer limit, Integer offset) {
        Page<Map<String, Object>> page = new Page(offset, limit);
        PageInfoBT pageInfoBT = new PageInfoBT(page);
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < ids.split(",").length; i++) {
            list.add(Integer.valueOf(ids.split(",")[i]));
        }
        List<Map<String, Object>> dots = tOrderService.selectZZOutList(page, list);
        pageInfoBT.setData(dots);
        pageInfoBT.setCount(page.getTotal());
        return pageInfoBT;
    }

    /**
     * 自提列表
     */
    @RequestMapping(value = "/selfLiftDataList")
    @ResponseBody
    public Object selfLiftDataList(@RequestParam(required = false) String ids, Integer limit, Integer offset) {
        Page<Map<String, Object>> page = new Page(offset, limit);
        PageInfoBT pageInfoBT = new PageInfoBT(page);
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < ids.split(",").length; i++) {
            list.add(Integer.valueOf(ids.split(",")[i]));
        }
        List<Map<String, Object>> dots = tOrderService.selectSelfLiftDataList(page, list);
        pageInfoBT.setData(dots);
        pageInfoBT.setCount(page.getTotal());
        return pageInfoBT;
    }

    /**
     * 跳转到修改订单管理
     */
    @RequestMapping("/the_print/{tOrderId}")
    public String thePrint(@PathVariable Integer tOrderId, Model model) {
        TOrder tOrder = tOrderService.selectById(tOrderId);
        Dot dot = dotMapper.selectById(tOrder.getDotId());
        Dept dept = deptMapper.selectById(dot.getDeptid());
        Date curDate = new Date();
        long time = curDate.getTime() - tOrder.getCreatetime().getTime();
        long days = time / (1000 * 60 * 60 * 24);
        List<Map<String, Object>> feeOrders = tOrderService.getOrderFee(tOrderId + "");

        model.addAttribute("days", days);
        model.addAttribute("feeOrders", feeOrders);
        List<Map<String, Object>> list = tOrderService.getFee(ShiroKit.getUser().getDeptId() + "");
        model.addAttribute("list", list);
        model.addAttribute("deptName", ShiroKit.getUser().getDeptName());
        model.addAttribute("dotName", dot.getDotName());
        model.addAttribute("dotAdress", dot.getDeliveryPoint());
        model.addAttribute("dotPhone", dot.getContactsPhone());
        model.addAttribute("getGoods", DateUtil.getTime());
        model.addAttribute("orderStaus", tOrder.getStatus());
        model.addAttribute("avatar", dept.getAvatar());
        model.addAttribute("creatTime", DateUtil.getTime(tOrder.getCreatetime()));
        model.addAttribute("modeName", tOrder.getMode().equals("0") ? "自提" : "配送");
        double sum = 0;
        String otherMoney = "";
        String bymoney = "";
        String ccmoney = "";
        String ldmoney = "";
        for (Map map : feeOrders) {
            if (map.get("use_object").toString().contains("3")) {
                if (map.get("fee_id").toString().equals("1")) {
                    tOrder.setPayment(Double.valueOf(map.get("money").toString()));
                } else if (map.get("fee_id").toString().equals("2")) {
                    tOrder.setCollection(Double.valueOf(map.get("money").toString()));
                } else if (map.get("fee_id").toString().equals("28")) {
                    bymoney = map.get("money").toString();
                } else if (map.get("fee_id").toString().equals("29")) {
                    ccmoney = map.get("money").toString();
                } else if (map.get("fee_id").toString().equals("41")) {
                    ldmoney = map.get("money").toString();
                } else {
                    otherMoney = otherMoney + map.get("fee_name").toString() + ":" + map.get("money").toString() + "; ";
                }
                sum = sum + Double.valueOf(map.get("money").toString());
            }
        }
        String pr = regionService.selectById(tOrder.getProvince_code()).getRegionName();
        String city = regionService.selectById(tOrder.getCity_code()).getRegionName();
        String are = regionService.selectById(tOrder.getArea_code()).getRegionName();
        tOrder.setReceivingAddress(pr + city + are + tOrder.getReceivingAddress());
        model.addAttribute("item", tOrder);
        model.addAttribute("money", sum);
        model.addAttribute("bymoney", bymoney);
        model.addAttribute("ccmoney", ccmoney);
        model.addAttribute("ldmoney", ldmoney);
        model.addAttribute("otherMoney", otherMoney);
        LogObjectHolder.me().set(tOrder);
        return PREFIX + "print_order.html";
    }

    @RequestMapping("/tTrainDisPrint/{tTrainId}")
    public String tTrainDisPrint(@PathVariable Integer tTrainId, Model model) {
        TTrain tTrain = tTrainService.selectById(tTrainId);

        Dept dept = deptMapper.selectById(ShiroKit.getUser().getDeptId());
        EntityWrapper<TOrder> wrapper = new EntityWrapper<>();
        wrapper.where("train_no={0}", tTrain.getTrainNo());
        List<TOrder> orders = tOrderService.selectList(wrapper);
        String ids = "";
        for (TOrder tOrder : orders) {
            ids = ids + tOrder.getId() + ",";
        }
        List<Map<String, Object>> list = tOrderService.getFee(ShiroKit.getUser().getDeptId() + "");
        Dot dot = dotMapper.selectById(tTrain.getDot_id());
        model.addAttribute("list", list);
        model.addAttribute("deptName", ShiroKit.getUser().getDeptName());
        model.addAttribute("dotName", dot.getDotName());
        model.addAttribute("dotAdress", dot.getDeliveryPoint());
        model.addAttribute("dotPhone", dot.getContactsPhone());
        model.addAttribute("getGoods", DateUtil.getTime());
        model.addAttribute("avatar", dept.getAvatar());
        model.addAttribute("ids", ids.substring(0, ids.length() - 1));
        model.addAttribute("creatTime", DateUtil.getTime(tTrain.getReportDate()));
        model.addAttribute("item", tTrain);
        LogObjectHolder.me().set(tTrain);
        return "/system/tOrder/orderDistribution/print_order.html";
    }


    @RequestMapping(value = "/addAndFefFee")
    @ResponseBody
    public Object addAndFefFee(String order_id, String fee_id, String value, String type) {
        EntityWrapper<TOrderFee> eWrapper = new EntityWrapper<TOrderFee>();
        eWrapper.where("order_id={0}", order_id).and("fee_id={0}", fee_id);
        List<TOrderFee> orderFees = orderFeeWarpper.selectList(eWrapper);
        TOrderFeeLog tOrderFeeLog = new TOrderFeeLog();
        if (orderFees != null && orderFees.size() > 0) {//更新费用
            TOrderFee tOrderFee = orderFees.get(0);
            tOrderFee.setMoney(Double.valueOf(value));
            tOrderFee.setCreateUserId(ShiroKit.getUser().getId());
            tOrderFee.setCreateTime(new Date());

            BeanUtils.copyProperties(tOrderFee, tOrderFeeLog);
            tOrderFeeLog.setOpUserId(ShiroKit.getUser().getId());
            tOrderFeeLog.setId(null);
            tOrderFeeLog.setStatus(0);
            tOrderFeeLog.setOrderFeeId(null);

//            orderFeeWarpper.updateById(tOrderFee);
        } else {
            int dot_id = userMapper.selectById(ShiroKit.getUser().getId()).getDot_id();
            TOrderFee tOrderFee = new TOrderFee();
            tOrderFee.setMoney(Double.valueOf(value));
            tOrderFee.setCreateUserId(ShiroKit.getUser().getId());
            tOrderFee.setCreateTime(new Date());
            tOrderFee.setSettlementType("0");
            tOrderFee.setAuditStatus(0);
            tOrderFee.setDotId(dot_id);
            if (!fee_id.equals("30")) {
                tOrderFee.setFeeIncome(0);
            } else {
                tOrderFee.setFeeIncome(1);
            }

            tOrderFee.setDetpId(ShiroKit.getUser().getDeptId());
            tOrderFee.setFeeId(Integer.valueOf(fee_id));
            tOrderFee.setOrderId(Integer.valueOf(order_id));
            BeanUtils.copyProperties(tOrderFee, tOrderFeeLog);
            tOrderFeeLog.setOpUserId(ShiroKit.getUser().getId());
            tOrderFeeLog.setId(null);
            tOrderFeeLog.setStatus(0);
            tOrderFeeLog.setOrderFeeId(null);
//            orderFeeWarpper.insert(tOrderFee);

        }
        //插入数据日志表，后续做正式表更新
        orderFeeLogMapper.insert(tOrderFeeLog);
        SUCCESS_TIP.setCode(tOrderFeeLog.getId());
        return super.SUCCESS_TIP;
    }

    //配送出库
    @RequestMapping(value = "/addAndTrian")
    @ResponseBody
    public Object addAndTrian(String ids, String car_no, String driver_name, String phone, String cf, String temOrderFeeS, String driverId) {
        Map param=new HashMap();
        param.put("dot_id",ShiroKit.getUser().getDotId());
        param.put("nType", Const.NTYPE_01); // 类别 00 初始 01 配送 02 中转 03 自提  04 月结
        String bath_no = tDictMapper.getNumCode(param);
        JSONArray jsonArray = JSONArray.parseArray(temOrderFeeS);
        String ecIds = getPara("ecIds");
        List<Map> maps = jsonArray.toJavaList(Map.class);
        TTrain tTrain = new TTrain();
        tTrain.setCarNo(car_no);
        tTrain.setDot_id(ShiroKit.getUser().getDotId());
        tTrain.setDriverName(driver_name);
        tTrain.setDriver_id(driverId);
        tTrain.setPhone(phone);
        tTrain.setSendDeptName(dotMapper.selectById(ShiroKit.getUser().getDotId()).getDotName());
        tTrain.setSendDeptId(ShiroKit.getUser().getDotId()+"");
        tTrain.setTrainNo(bath_no);
        tTrain.setTrain_type("2");//0 初始 1 自提 2配送 3中转
        tTrain.setStatus(2);// 0 未核销 1 核销 2 配送  3已付款
        tTrain.setUserId(ShiroKit.getUser().getId());
        tTrain.setCreateTime(new Date());
        tTrain.setReportDate(new Date());
        tOrderService.addAndTrian(ids, tTrain, cf, maps);
        if (!StringUtils.isEmpty(ecIds)) {
            String[] tIds = ecIds.split(",");
            for (int i = 0; i < tIds.length; i++) {
                //这个写的不好。。。
                TOrder order = new TOrder();
                order.setId(Integer.valueOf(tIds[i]));
                order.setTrain_no("");
                order.setTrain_id(-1);
                order.setStatus(1);
                order.setFlow_status(1);//0 开单 1 车次核销 2 出库 3 收银 4 核销
                tOrderService.updateById(order);
            }
        }
        SUCCESS_TIP.setCode(tTrain.getId());
        return super.SUCCESS_TIP;
    }

    /**
     * 获取订单管理列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(@RequestParam(required = false) String name,
                       Integer limit, Integer offset, String audit_status, String receipt_status) {
        Page<Map<String, Object>> page = new Page(offset, limit);
        PageInfoBT pageInfoBT = new PageInfoBT(page);
        String dot_id = userMapper.selectById(ShiroKit.getUser().getId()).getDot_id() + "";
        if (StringUtils.isEmpty(audit_status)) {
            audit_status = "1";
        }

        List<Map<String, Object>> dots = tOrderService.selectZtAuditeds(page, dot_id, name, audit_status, receipt_status);
        pageInfoBT.setData(dots);
        pageInfoBT.setCount(page.getTotal());
        return pageInfoBT;

    }

    @RequestMapping(value = "/listTransfer")
    @ResponseBody
    public Object listTransfer(@RequestParam(required = false) String name,
                               Integer limit, Integer offset, String province_code, String city_code, String area_code, String receipt_status, String status) {
        Page<Map<String, Object>> page = new Page(offset, limit);
        PageInfoBT pageInfoBT = new PageInfoBT(page);
        String dot_id = userMapper.selectById(ShiroKit.getUser().getId()).getDot_id() + "";
        String mRStatus = "";
        String receipt = "";
//        String tatus = "";
        if (receipt_status != null) {
            if (receipt_status.contains("1")) {
                mRStatus = "mRStatus";
            } else if (receipt_status.contains("2")) {
                receipt = "receipt";
            }
        }
        if (StringUtils.isEmpty(status)) {
            status = "1";
        }
        List<Map<String, Object>> dots = tOrderService.selectTransfer(page, dot_id, name, mRStatus, receipt, status, province_code, city_code, area_code);
        new OrderWarpper(dots).warp();
        pageInfoBT.setData(dots);
        pageInfoBT.setCount(page.getTotal());
        return pageInfoBT;
    }


    /**
     * 跳转到修改订单管理
     */

    @RequestMapping("/dis_print")
    public String disPrint(String ids, Model model) {
        model.addAttribute("getOrderTime", DateUtil.getDay());
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < ids.split(",").length; i++) {
            list.add(Integer.valueOf(ids.split(",")[i]));
        }
        List<Map<String, Object>> maps = tOrderService.getTransferOrders(list);


        int dot_id = userMapper.selectById(ShiroKit.getUser().getId()).getDot_id();
        Dot dot = dotMapper.selectById(dot_id);
        Dept dept = deptMapper.selectById(dot.getDeptid());
        model.addAttribute("dotName", dotMapper.selectById(dot_id).getDotName());
        model.addAttribute("dotAdress", dotMapper.selectById(dot_id).getDeliveryPoint());
        model.addAttribute("dotPhone", dotMapper.selectById(dot_id).getContactsPhone());
        model.addAttribute("deptName", ShiroKit.getUser().getDeptName());
        model.addAttribute("dotId", dot_id);
        model.addAttribute("zzdotName", maps.get(0).get("transfer_name"));
        model.addAttribute("driverName", maps.get(0).get("really_user"));
        model.addAttribute("card", maps.get(0).get("really_card"));
        model.addAttribute("carNo", maps.get(0).get("car_no"));
        model.addAttribute("status", maps.get(0).get("status"));
        model.addAttribute("avatar", dept.getAvatar());
        model.addAttribute("maps", maps);
        model.addAttribute("ids", ids);


        return PREFIX + "orderDistribution/print_order.html";
    }


    /**
     * 跳转到修改订单管理
     */

    @RequestMapping("/transfer_print/{tTrainId}")
    public String transferPrint(@PathVariable Integer tTrainId, Model model) {
        TTrain tTrain = tTrainService.selectById(tTrainId);
        Dept dept = deptMapper.selectById(ShiroKit.getUser().getDeptId());
        EntityWrapper<TOrder> wrapper = new EntityWrapper<>();
        wrapper.where("train_no={0}", tTrain.getTrainNo());
        List<TOrder> orders = tOrderService.selectList(wrapper);
        String ids = "";
        for (TOrder tOrder : orders) {
            ids = ids + tOrder.getId() + ",";
        }
        Dot dot = dotMapper.selectById(tTrain.getDot_id());
        List<Map<String, Object>> list = tOrderService.getFee(ShiroKit.getUser().getDeptId() + "");
        model.addAttribute("list", list);
        model.addAttribute("deptName", ShiroKit.getUser().getDeptName());
        model.addAttribute("dotName", dot.getDotName());
        model.addAttribute("dotAdress", dot.getDeliveryPoint());
        model.addAttribute("dotPhone", dot.getContactsPhone());
        model.addAttribute("getGoods", DateUtil.getTime());
        model.addAttribute("avatar", dept.getAvatar());
        model.addAttribute("ids", ids.substring(0, ids.length() - 1));
        model.addAttribute("creatTime", DateUtil.getTime(tTrain.getReportDate()));
        model.addAttribute("item", tTrain);
        LogObjectHolder.me().set(tTrain);
        return PREFIX + "transfer/print_order.html";
    }


    /**
     * 获取订单管理列表
     */
    @RequestMapping(value = "/outdisList")
    @ResponseBody
    public Object outdisList(@RequestParam(required = false) String name, String is_out, String receipt_status, String flag, String status) {
        Page<Map<String, Object>> page = new PageFactory<Map<String, Object>>().defaultPage();
        PageInfoBT pageInfoBT = new PageInfoBT(page);
        String mRStatus = "";
        String receipt = "";
        if (receipt_status != null) {
            if (receipt_status.contains("1")) {
                mRStatus = "mRStatus";
            } else if (receipt_status.contains("2")) {
                receipt = "receipt";
            }
        }
        if (StringUtils.isEmpty(status)) {
            status = "1";
        }
        List<Map<String, Object>> dots = tOrderService.selectZtNoOut(page, ShiroKit.getUser().getDotId()+"", name, is_out, mRStatus, receipt, status, flag);
        new OrderWarpper(dots).warp();
        pageInfoBT.setRows(dots);
        pageInfoBT.setTotal(page.getTotal());
        return pageInfoBT;

    }

    /**
     * 获取订单管理列表
     */
    @RequestMapping(value = "/outTransferList")
    @ResponseBody
    public Object outTransferList(@RequestParam(required = false) String name, String receipt_status, String status) {
        Page<Map<String, Object>> page = new PageFactory<Map<String, Object>>().defaultPage();
        PageInfoBT pageInfoBT = new PageInfoBT(page);
        if (StringUtils.isEmpty(status)) {
            status = "1";
        }
        String privince_code = "";
        String city_code = "";
        String area_code = "";
        String region = getPara("region");
//        if (region != null && !region.isEmpty()) {
//            Map map = iRegionService.selectReglionId(region);
//            if (map != null) {
//                String str[] = map.get("reg").toString().split(",");
//                if (str[2].equals("2")) {
//                    city_code = str[0];
//                    privince_code = str[1];
//                    area_code = str[3];
//                } else {
//                    city_code = str[1];
//                    privince_code = str[3];
//                    area_code = str[0];
//                }
//            }
//        }
        List<Map<String, Object>> dots = tOrderService.selectTransfer(page, ShiroKit.getUser().getDotId() + "", name, receipt_status, "", status, privince_code, city_code, region);
        new OrderWarpper(dots).warp();
        pageInfoBT.setRows(dots);
        pageInfoBT.setTotal(page.getTotal());
        return pageInfoBT;

    }


    /**
     * 自提管理列表数据
     */
    @RequestMapping(value = "/outList")
    @ResponseBody
    public Object outList(@RequestParam(required = false) String name, Integer limit, Integer offset, String is_out, String receipt_status, String flag, String status) {

        Page<Map<String, Object>> page = new Page(offset, limit);
        PageInfoBT pageInfoBT = new PageInfoBT(page);
        String mRStatus = "";
        String receipt = "";
        if (receipt_status != null) {
            if (receipt_status.contains("1")) {
                mRStatus = "mRStatus";
            } else if (receipt_status.contains("2")) {
                receipt = "receipt";
            }
        }
        if (StringUtils.isEmpty(status)) {
            status = "1";
        }
        List<Map<String, Object>> dots = tOrderService.selectSelfLiftInStoreHouse(page, ShiroKit.getUser().getDotId() + "", name, is_out, mRStatus, receipt, status, flag);
        new OrderWarpper(dots).warp();
        pageInfoBT.setData(dots);
        pageInfoBT.setCount(page.getTotal());
        return pageInfoBT;
    }


    /**
     * 批量自提管理列表数据
     */
    @RequestMapping(value = "/batchOutListData")
    @ResponseBody
    public Object batchOutListData(@RequestParam(required = false) String name, String is_out, String receipt_status, String flag, String status) {
        Page<Map<String, Object>> page = new PageFactory<Map<String, Object>>().defaultPage();
        PageInfoBT pageInfoBT = new PageInfoBT(page);
        String mRStatus = "";
        String receipt = "";
        if (receipt_status != null) {
            if (receipt_status.contains("1")) {
                mRStatus = "mRStatus";
            } else if (receipt_status.contains("2")) {
                receipt = "receipt";
            }
        }
        if (StringUtils.isEmpty(status)) {
            status = "1";
        }
        List<Map<String, Object>> dots = tOrderService.selectSelfLiftInStoreHouse(page, ShiroKit.getUser().getDotId() + "", name, is_out, mRStatus, receipt, status, flag);
        new OrderWarpper(dots).warp();
//        pageInfoBT.setData(dots);
//        pageInfoBT.setCount(page.getTotal());
        pageInfoBT.setRows(dots);
        pageInfoBT.setTotal(page.getTotal());
        return pageInfoBT;

    }

    /**
     * 新增订单管理
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(TOrder tOrder) {
        tOrderService.insert(tOrder);
        return super.SUCCESS_TIP;
    }

    /**
     * 删除订单管理
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer tOrderId) {
        tOrderService.deleteById(tOrderId);
        return SUCCESS_TIP;
    }


    /**
     * 强制清账
     */
    @RequestMapping(value = "/updateStatus")
    @ResponseBody
    public Object updateStatus(@RequestParam Integer tOrderId) {
//        tOrderService.deleteById(tOrderId);
//
        Wrapper<TOrderFee> wrapper = new EntityWrapper<>();
        wrapper = wrapper.where("order_id={0} ", tOrderId).andNew("audit_status={0}", 2);
        List<TOrderFee> orderFees = orderFeeWarpper.selectList(wrapper);
        for (TOrderFee tOrderFee : orderFees) {
            String no = DateUtil.getMsTime();
            orderMapper.addOrderFeeLog(no, ShiroKit.getUser().getId() + "", tOrderFee.getId() + "");
            tOrderFee.setAuditStatus(3);
            orderFeeWarpper.updateById(tOrderFee);
        }
        return SUCCESS_TIP;
    }

    /**
     * 修改订单管理
     */
    @RequestMapping(value = "/update")
    @BussinessLog(value = "订单修改", key = "orderNum", dict = OrderDict.class)
    @ResponseBody
    public Object update(TOrder tOrder) {
        if (tOrder.getMode() == 2) {
            tOrder.setIsTransfer("1");
        }
        tOrderService.updateById(tOrder);
        return super.SUCCESS_TIP;
    }

    /**
     * 修改订单管理
     */
    @RequestMapping(value = "/checkOrder")
    @ResponseBody
    public Object checkOrder(String ids) {
        String mId[] = ids.split(",");
        for (int i = 0; i < mId.length; i++) {
            tOrderService.checkOrder(Integer.valueOf(mId[i]), ShiroKit.getUser().getDeptId(), ShiroKit.getUser().getDotId()+"", ShiroKit.getUser().getId());
        }
        return super.SUCCESS_TIP;
    }

    @RequestMapping(value = "/sendMsg")
    @ResponseBody
    public Object sendMsg(String ids) {
        String mId[] = ids.split(",");
        Dot dot = dotMapper.selectById(Integer.valueOf(ShiroKit.getUser().getDotId()));
        for (int i = 0; i < mId.length; i++) {
            TOrder order = orderMapper.selectById(Integer.valueOf(mId[i]));
            Dept dept = deptMapper.selectById(ShiroKit.getUser().getDeptId());
            String str = "【鑫合顺】您好，您的" + order.getGoodsName() + "已到网点" + dot.getDotName() + "，请尽快来取。联系电话" + dot.getContactsPhone() + "。提货地址:" + dot.getDeliveryPoint() + "。货物超过三天加收仓储费。配送每票50元起";
            // String str = "【鑫合顺】：您好，您的"+order.getGoodsName()+"，已到网点" + dot.getDotName() + ",请尽快来取。联系电话（" + dot.getContactsPhone() + "）。提货地址（" + dot.getDeliveryPoint() + "）。货物超过三天加收仓储费。配送每票50元起";
            int smsNum = dept.getMessage_num();
            TOrder order3 = new TOrder();
            order3.setId(Integer.valueOf(mId[i]));
            order3.setPush_str(str);
            if (smsNum > 0) {
                SmsSingleSenderResult result = TencentSmsUtil.pushSmsToSingle(order.getReceivingPhone(), str);
                if (result.result == 0) {
                    TNotice tNotice=new TNotice();
                    tNotice.setCreateUsr(ShiroKit.getUser().getId());
                    tNotice.setMsgStatus(0);
                    tNotice.setMsgStr(str);
                    tNotice.setMsgType(0);
                    tNotice.setOrderId(order3.getId());
                    order3.setCall_str("短信通知成功");
                    order3.setNotification_time(new Date());
                    Map map1 = new HashMap();
                    map1.put("dept_id", ShiroKit.getUser().getDeptId());
                    deptMapper.smsNum(map1);
                    noticeMapper.insert(tNotice);
                } else {
                    order3.setCall_str(result.errMsg);
                }
            } else {
                order3.setCall_str("短信数量不够");
            }
            orderMapper.updateById(order3);
        }
//
//
//
//
//        YunpianConf yunpianConf = new YunpianConf().build();
//        YunpianClient yp = new YunpianClient().init();
//        String dot_id = userMapper.selectById(ShiroKit.getUser().getId()).getDot_id() + "";
//      //  Dot dot = dotMapper.selectById(Integer.valueOf(dot_id));
//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//
//                Map map1 = new HashMap();
//                map1.put("dept_id", ShiroKit.getUser().getDeptId());
//                Map<String, Object> push = orderMapper.selectPush(ShiroKit.getUser().getDeptId() + "");
//                String mId[] = ids.split(",");
//                for (int i = 0; i < mId.length; i++) {
//                    TOrder order = orderMapper.selectById(Integer.valueOf(mId[i]));
//                    Dept dept = deptMapper.selectById(ShiroKit.getUser().getDeptId());
//
//                    String str = "物流信息：您好，您的"+order.getGoodsName()+"物品，已到网点" + dot.getDotName() + ",请尽快来取。联系电话（" + dot.getContactsPhone() + "）。提货地址（" + dot.getDeliveryPoint() + "）。货物超过三天加收仓储费。配送每票50元起";
//                    int smsNum = dept.getMessage_num();
//                    TOrder order3 = new TOrder();
//                    order3.setId(Integer.valueOf(mId[i]));
//                    order3.setPush_str(str);
//                    if (smsNum > 0) {
//                        Map<String, String> params = new HashMap<String, String>();
//                        params.put("apikey", yunpianConf.getApikey());
//                        params.put("text", str);
//                        params.put("mobile", order.getReceivingPhone());
//                        params.put("uid", order.getOrderNum() + "|" + push.get("push_model") + "|" + mId[i]);
//                        params.put("callback_url", "");
//                        Result<SmsSingleSend> sendResult = yp.sms().single_send(params);
//                        if (sendResult.getCode() == 0) {
//                            order3.setCall_str("短信通知成功");
//                            deptMapper.smsNum(map1);
//                        } else {
//                            order3.setCall_str(sendResult.getMsg());
//                        }
//                    } else {
//                        order3.setCall_str("短信数量不够");
//                    }
//                    orderMapper.updateById(order3);
//                }
//                yp.close();
//            }
//        }).start();
        return super.SUCCESS_TIP;
    }

    @RequestMapping(value = "/callPhone")
    @ResponseBody
    public Object callPhone(String ids) {

        String mId[] = ids.split(",");
        Dot dot = dotMapper.selectById(Integer.valueOf(ShiroKit.getUser().getDotId()));
        for (int i = 0; i < mId.length; i++) {
            TOrder order = orderMapper.selectById(Integer.valueOf(mId[i]));
            Dept dept = deptMapper.selectById(ShiroKit.getUser().getDeptId());

            String str = "【鑫合顺】您好，您的" + order.getGoodsName() + "已到网点" + dot.getDotName() + "，请尽快来取。联系电话" + dot.getContactsPhone() + "。提货地址:" + dot.getDeliveryPoint() + "。货物超过三天加收仓储费。配送每票50元起";
            int smsNum = dept.getCall_num();
            TOrder order3 = new TOrder();
            order3.setId(Integer.valueOf(mId[i]));
            order3.setPush_str(str);
            if (smsNum > 0) {
                SmsVoiceVerifyCodeSenderResult result = TencentSmsUtil.pushVoiceToSingle(order.getReceivingPhone(), str);
                if (result.result == 0) {

                    TNotice tNotice=new TNotice();
                    tNotice.setCreateUsr(ShiroKit.getUser().getId());
                    tNotice.setMsgStatus(0);
                    tNotice.setMsgStr(str);
                    tNotice.setMsgType(1);
                    tNotice.setOrderId(order3.getId());
                    noticeMapper.insert(tNotice);
                    order3.setCall_str("语音通知成功");
                    order3.setNotification_time(new Date());
                    Map map1 = new HashMap();
                    map1.put("dept_id", ShiroKit.getUser().getDeptId());
                    deptMapper.smsNum(map1);
                } else {
                    order3.setCall_str(result.errMsg);
                }
            } else {
                order3.setCall_str("语音数量不够");
            }
            orderMapper.updateById(order3);
        }
//
//        String ENCODING = "UTF-8";
//
//        YunpianConf yunpianConf = new YunpianConf().build();
//        YunpianClient yp = new YunpianClient().init();
//
//
//        String dot_id = userMapper.selectById(ShiroKit.getUser().getId()).getDot_id() + "";
//        Dot dot = dotMapper.selectById(Integer.valueOf(dot_id));
//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//                String tpl_value = null;
//                Map map1 = new HashMap();
//                map1.put("dept_id", ShiroKit.getUser().getDeptId());
//                Map<String, Object> push = orderMapper.selectPush(ShiroKit.getUser().getDeptId() + "");
//                String mId[] = ids.split(",");
//                for (int i = 0; i < mId.length; i++) {
//                    TOrder order = orderMapper.selectById(Integer.valueOf(mId[i]));
//                    try {
//                        tpl_value = URLEncoder.encode("#20#", ENCODING) + "=" +
//                                URLEncoder.encode(order.getOrderNum(), ENCODING) + "&" + URLEncoder.encode("#21#", ENCODING) + "=" +
//                                URLEncoder.encode(dot.getDotName(), ENCODING) + "&" + URLEncoder.encode("#22#", ENCODING) + "=" +
//                                URLEncoder.encode(dot.getContactsPhone(), ENCODING) + "&" + URLEncoder.encode("#23#", ENCODING) + "=" +
//                                URLEncoder.encode(dot.getDeliveryPoint(), ENCODING);
//                    } catch (UnsupportedEncodingException e) {
//                        e.printStackTrace();
//                    }
//                    Dept dept = deptMapper.selectById(ShiroKit.getUser().getDeptId());
//                   // String str = "您好，订单编号" + order.getOrderNum() + "_物品，已到网点" + dot.getDotName() + ",请尽快来取。联系电话（" + dot.getContactsPhone() + "）。提货地址（" + dot.getDeliveryPoint() + "）";
//                    String str = "物流信息：您好，您的"+order.getGoodsName()+"物品，已到网点" + dot.getDotName() + ",请尽快来取。联系电话（" + dot.getContactsPhone() + "）。提货地址（" + dot.getDeliveryPoint() + "）。货物超过三天加收仓储费。配送每票50元起";
//
//                    int smsNum = dept.getCall_num();
//                    TOrder order3 = new TOrder();
//                    order3.setId(Integer.valueOf(mId[i]));
//                    order3.setPush_str(str);
//                    if (smsNum > 0) {
//                        Map<String, String> params = new HashMap<String, String>();
//                        params.put("apikey", yunpianConf.getApikey());
//                        params.put("mobile", order.getReceivingPhone());
//                        params.put("tpl_id", "2179822");
//                        params.put("tpl_value", tpl_value);
//                        params.put("callback_url", "");
//                        params.put("uid", order.getOrderNum() + "|" + push.get("push_model") + "|" + mId[i]);
//                        Result<VoiceSend> sendResult = yp.voice().tpl_notify(params);
//                        if (sendResult.getCode() == 0) {
//                            order3.setPush_str(tpl_value);
//                            order3.setCall_str("语音通知成功");
//                            deptMapper.smsCall(map1);
//                        } else {
//                            order3.setCall_str(sendResult.getMsg());
//                        }
//                    } else {
//                        order3.setCall_str("语音数量不够");
//                    }
//                    orderMapper.updateById(order3);
//                }
//                yp.close();
//            }
//        }).start();


        return super.SUCCESS_TIP;
    }


    /**
     * 回单
     */
    @RequestMapping(value = "/orderReceipt")
    @ResponseBody
    public Object orderReceipt(String tOrderId) {
        String mId[] = tOrderId.split(",");
        for (int i = 0; i < mId.length; i++) {
            TOrder order = new TOrder();
            order.setId(Integer.valueOf(mId[i]));
            order.setReceiptStatus(1);
            Map map = orderMapper.selectMinOrderStatus(tOrderId + "");
            if (map.get("audit_status").toString().equals("1")) {
                order.setStatus(3);
            }
            tOrderService.updateById(order);
        }

        return super.SUCCESS_TIP;
    }

    @RequestMapping(value = "/orderTipStaus")
    @ResponseBody
    public Object orderTipStaus(String tOrderId, String isOut) {

        String mId[] = tOrderId.split(",");
        for (int i = 0; i < mId.length; i++) {
            TOrder order = new TOrder();
            order.setId(Integer.valueOf(mId[i]));
            if (isOut.equals("0")) {
                order.setNotice("手工通知");
            } else {
                order.setNotice("失联");
            }
            order.setIsOut(isOut);
            tOrderService.updateById(order);
        }
        return super.SUCCESS_TIP;
    }

    /**
     * 修改订单管理
     */
    @RequestMapping(value = "/refOrderFee")
    @ResponseBody
    public Object refOrderFee(String str, String order_id) {

        JSONArray jsonArray = JSONArray.parseArray(str);
        String no = DateUtil.getMsTime();
        List<Map> maps = jsonArray.toJavaList(Map.class);
        tOrderService.opOrderFee(maps, ShiroKit.getUser().getDeptId(), ShiroKit.getUser().getId(), no, order_id);
        return super.SUCCESS_TIP;
    }

    @RequestMapping(value = "/refSuperOrderFee")
    @ResponseBody
    public Object refSuperOrderFee(String str, String order_id) {

        JSONArray jsonArray = JSONArray.parseArray(str);
        String no = DateUtil.getMsTime();
        List<Map> maps = jsonArray.toJavaList(Map.class);
        tOrderService.opOrderFeeSuper(maps, ShiroKit.getUser().getDeptId(), ShiroKit.getUser().getId(), no, order_id);
        return super.SUCCESS_TIP;
    }


    @RequestMapping(value = "/printOrder")
    @ResponseBody
    public Object printOrder(TOrder order) {

//        order.setStatus(2);
        tOrderService.updateById(order);

        return super.SUCCESS_TIP;
    }


    @RequestMapping(value = "/printTransefrOrder")
    @ResponseBody
    public Object printTransefrOrder(String ids, String driverId, TOrder order) {

        String mId[] = ids.split(",");
        for (int i = 0; i < mId.length; i++) {
            order.setStatus(2);
            order.setId(Integer.valueOf(mId[i]));
            if (driverId != null) {
                order.setDriverId(Integer.valueOf(driverId));
            }
            tOrderService.updateById(order);
        }


        return super.SUCCESS_TIP;
    }

    /**
     * 运单总表列表
     */
    @RequestMapping("/getSummaryOrders")
    public String getSummaryOrders() {
        return PREFIX + "summaryOrders.html";
    }


    @RequestMapping(value = "/summaryList")
    @ResponseBody
    public Object summaryList(Integer limit, Integer offset) {

        Page<Map<String, Object>> page = new Page(offset, limit);
        PageInfoBT pageInfoBT = new PageInfoBT(page);
        Map map = HttpKit.getRequestParameters();
        String flag=getPara("flag")==null?"":getPara("flag").toString();
        if(!StringUtils.isEmpty(flag)){
            String is_transfer = "";
            String mode="";
            if (map.get("dj_type") != null&&!map.get("dj_type").toString().isEmpty()) {
                if(map.get("dj_type").toString().contains("3")){
                    is_transfer="1";
                } else {
                    is_transfer = "0";
                    mode = map.get("dj_type").toString();
                }
            }
            map.put("dot_id", ShiroKit.getUser().getDotId());
            map.put("is_transfer", is_transfer);
            map.put("mode", mode);

            String region=getPara("region");
            String privince_code="";
            String city_code="";
            String area_code="";
            if(region!=null&&!region.isEmpty()){
                Map map1=iRegionService.selectReglionId(region);
                if(map1!=null){
                    String str[]=map1.get("reg").toString().split(",");
                    if(str[2].equals("2")){
                        city_code=str[0];
                        privince_code=str[1];
                        area_code=str[3];
                    }else{
                        city_code=str[1];
                        privince_code=str[3];
                        area_code=str[0];
                    }
                    map.put("privince_code", privince_code);
                    map.put("city_code", city_code);
                    map.put("area_code", area_code);
                }
            }
            List<Map<String, Object>> dots = tOrderService.selectReport(page, map);
            new OrderWarpper(dots).warp();
            pageInfoBT.setData(dots);
            pageInfoBT.setCount(page.getTotal());
        }
        return pageInfoBT;
    }

    @RequestMapping(value = "/summaryListAvg")
    @ResponseBody
    public Object summaryListAvg() {
        Map map = HttpKit.getRequestParameters();
        String flag=getPara("flag")==null?"":getPara("flag").toString();
        if(!StringUtils.isEmpty(flag)){
            String is_transfer = "";
            String mode="";
            if (map.get("dj_type") != null&&!map.get("dj_type").toString().isEmpty()) {
                if(map.get("dj_type").toString().contains("3")){
                    is_transfer="1";
                } else {
                    is_transfer = "0";
                    mode = map.get("dj_type").toString();
                }
            }
            map.put("dot_id", ShiroKit.getUser().getDotId());
            map.put("is_transfer", is_transfer);
            map.put("mode", mode);

            String region=getPara("region");
            String privince_code="";
            String city_code="";
            String area_code="";
            if(region!=null&&!region.isEmpty()){
                Map map1=iRegionService.selectReglionId(region);
                if(map1!=null){
                    String str[]=map1.get("reg").toString().split(",");
                    if(str[2].equals("2")){
                        city_code=str[0];
                        privince_code=str[1];
                        area_code=str[3];
                    }else{
                        city_code=str[1];
                        privince_code=str[3];
                        area_code=str[0];
                    }
                    map.put("privince_code", privince_code);
                    map.put("city_code", city_code);
                    map.put("area_code", area_code);
                }
            }
            List<Map<String, Object>> dots = tOrderService.selectReportAvg(map);
            return dots;
        }
        return null;
    }

    @RequestMapping(value = "/exportSummaryList")
    @ResponseBody
    public Object exportSummaryList() {
        Page<Map<String, Object>> page = new Page(0, 50000);
        Map map = HttpKit.getRequestParameters();
        String is_transfer = "";
        String mode="";
        if (map.get("dj_type") != null&&!map.get("dj_type").toString().isEmpty()) {
            if(map.get("dj_type").toString().contains("3")){
                is_transfer="1";
            } else {
                is_transfer = "0";
                mode = map.get("dj_type").toString();
            }
        }
        map.put("dot_id", ShiroKit.getUser().getDotId());
        map.put("is_transfer", is_transfer);
        map.put("mode", mode);

        String region=getPara("region");
        String privince_code="";
        String city_code="";
        String area_code="";
        if(region!=null&&!region.isEmpty()){
            Map map1=iRegionService.selectReglionId(region);
            if(map1!=null){
                String str[]=map1.get("reg").toString().split(",");
                if(str[2].equals("2")){
                    city_code=str[0];
                    privince_code=str[1];
                    area_code=str[3];
                }else{
                    city_code=str[1];
                    privince_code=str[3];
                    area_code=str[0];
                }
                map.put("privince_code", privince_code);
                map.put("city_code", city_code);
                map.put("area_code", area_code);
            }
        }
        List<Map<String, Object>> dots = tOrderService.selectReport(page, map);
        new OrderWarpper(dots).warp();

        String dirPath = HttpKit.getRequest().getSession().getServletContext().getRealPath("/") + Const.FILE_PATH;
        String dateDir = new SimpleDateFormat("yyyyMMdd").format(new java.util.Date());

        File dir = new File(dirPath + "/" + dateDir);
        if (!dir.exists()) {
            dir.mkdirs();
        }
        String datePath = "/运单总表"+new SimpleDateFormat("yyyyMMddHHmmss").format(new java.util.Date())+".xls";
        tOrderService.export(dirPath + "/" + dateDir+datePath,"/"+Const.FILE_PATH+"/" + dateDir+datePath,dots);
        return renderFile("运单总表"+new SimpleDateFormat("yyyyMMddHHmmss").format(new java.util.Date())+".xls",dirPath + "/" + dateDir+datePath);
    }


    @RequestMapping("/orderOut")
    @ResponseBody
    public Object orderOut(TOrder order, String payMode) {
        String cash = getPara("cash");
        String weixin = getPara("weixin");
        String alipay = getPara("alipay");
        String bank = getPara("bank");
        String xj = getPara("xj");
        order.setOut_time(new Date());

        String billNum = "TT" + ShiroKit.getUser().getId() + "_" + DateUtil.getAllTimes();
        ;
        TBill bill = new TBill();
        bill.setAlipay(Float.valueOf(alipay));
        bill.setWeixin(Float.valueOf(weixin));
        bill.setBank(Float.valueOf(bank));
        bill.setCash(Float.valueOf(cash));
        bill.setDotId(ShiroKit.getUser().getDotId());
        bill.setEditUser(ShiroKit.getUser().getId());
        bill.setCreateTime(new Date());
        bill.setBillNum(billNum);
        if (xj != null && "0".equals(xj)) {
            //不产生费用无需核销
            order.setStatus(3);
            order.setFlow_status(4);//0 开单 1 车次核销 2 出库 3 收银 4 核销
        } else {
            order.setStatus(2);
            order.setFlow_status(3);//0 开单 1 车次核销 2 出库 3 收银 4 核销
        }
        itOrderFeeService.updateOrderFee(order.getId() + "", "3", bill, order, xj);
        return super.SUCCESS_TIP;
    }

    /**
     * 自提单批量出库
     *
     * @return
     */
    @RequestMapping("/selfLiftOrderBatchOut")
    @ResponseBody
    public Object orderOut() {
        String cash = getPara("cash");
        String weixin = getPara("weixin");
        String alipay = getPara("alipay");
        String bank = getPara("bank");
        String xj = getPara("xj");
        String ids = getPara("ids");
        String really_user = getPara("really_user");
        String really_card = getPara("really_card");
        String[] orderids = ids.split(",");

        String billNum = "ZT" + ShiroKit.getUser().getId() + "_" + DateUtil.getAllTimes();
        ;
        TBill bill = new TBill();
        bill.setAlipay(Float.valueOf(alipay));
        bill.setWeixin(Float.valueOf(weixin));
        bill.setBank(Float.valueOf(bank));
        bill.setCash(Float.valueOf(cash));
        bill.setDotId(ShiroKit.getUser().getDotId());
        bill.setEditUser(ShiroKit.getUser().getId());
        bill.setCreateTime(new Date());
        bill.setBillNum(billNum);
        List<TOrder> list = new ArrayList<TOrder>();
        for (String id : orderids) {
            TOrder order = new TOrder();
            order.setId(Integer.parseInt(id));
            order.setReallyUser(really_user);
            order.setReallyCard(really_card);
            if (xj != null && "0".equals(xj)) {
                //不产生费用无需核销
                order.setStatus(3);
                order.setFlow_status(4); //0 开单 1 车次核销 2 出库 3 收银 4 核销
            } else {
                order.setStatus(2);
                order.setFlow_status(3);//0 开单 1 车次核销 2 出库 3 收银 4 核销
            }
            list.add(order);
        }


        itOrderFeeService.updateSelfLiftFees(list, bill, xj);
        return super.SUCCESS_TIP;
    }

    @RequestMapping("/orderZTHX")
    @ResponseBody
    public Object orderZTHX(String order_id, String staus) {
        TOrder order = new TOrder();
        order.setId(Integer.valueOf(order_id));
        order.setFlow_status(4);//0 开单 1 车次核销 2 出库 3 收银 4 核销
        EntityWrapper<TOrderFee> eWrapper = new EntityWrapper<TOrderFee>();
        eWrapper.where("order_id={0}", order_id + "");
        List<TOrderFee> orderFees = orderFeeWarpper.selectList(eWrapper);
        for (TOrderFee tOrderFee : orderFees) {
            tOrderFee.setAuditStatus(3);
            //todo 操作人
            itOrderFeeService.updateById(tOrderFee);
        }
        orderMapper.updateById(order);
        return super.SUCCESS_TIP;
    }


    @RequestMapping("/orderSuperRef")
    @ResponseBody
    public Object orderSuperRef(String order_id, String staus) {

//        tOrderService.updateById(order);

        //todo 记得如果自提有仓存、搬运 费用的话有生成 代收、提付也有重新生成

        return super.SUCCESS_TIP;
    }

    @RequestMapping("/getSuperOrders")
    public String getSuperOrders(Model model) {

        return PREFIX + "superOrder/superOrders.html";
    }

    @RequestMapping(value = "/superOrderList")
    @ResponseBody
    public Object superOrderList(Integer limit, Integer offset) {

        Page<Map<String, Object>> page = new Page(offset, limit);
        PageInfoBT pageInfoBT = new PageInfoBT(page);
        Map map = HttpKit.getRequestParameters();
        String is_transfer = "";
        if (map.get("dj_type") != null) {
            is_transfer = map.get("dj_type").toString().contains("3") ? "1" : "";
        }

        User user = userMapper.selectById(ShiroKit.getUser().getId());
        Dot dot = dotMapper.selectById(user.getDot_id());
        map.put("dot_id", dot.getId());
        map.put("is_transfer", is_transfer);
//        map.put("mode",map.get("dj_type").toString());
        List<Map<String, Object>> dots = tOrderService.selectSuperOrder(page, map);
        new OrderWarpper(dots).warp();
        pageInfoBT.setData(dots);
        pageInfoBT.setCount(page.getTotal());
        return pageInfoBT;
    }


    @RequestMapping(value = "/orderIndex")
    @ResponseBody
    public Object orderIndex() {
        User user = userMapper.selectById(ShiroKit.getUser().getId());
        Map param = new HashMap();
        param.put("dot_id", user.getDot_id());
        Map map = tOrderService.indexNum(param);

        return map;
    }


    /**
     * 跳转到修改订单费用
     */
    @RequestMapping("/summaryDetail/{tOrderId}")
    public String summaryDetail(@PathVariable Integer tOrderId, Model model) {
        TOrder tOrder = tOrderService.selectById(tOrderId);
        TTrain tTrain = tTrainService.selectById(tOrder.getBacth_id());//开单车次
        TTrain tTrain1 = tTrainService.selectById(tOrder.getTrain_id());//中转车次
        Date curDate = new Date();
        long time = curDate.getTime() - tOrder.getCreatetime().getTime();
        long days = time / (1000 * 60 * 60 * 24);
        EntityWrapper<TOrderFee> entityWrapper = new EntityWrapper<>();
        entityWrapper.where("order_id={0}", tOrderId);
        List<TOrderFee> feeOrders = itOrderFeeService.selectList(entityWrapper);

        OrderBackRecord orderBackRecordReceive = new OrderBackRecord();
        if (tOrder.getOrder_back_receive_id() != null) {
            orderBackRecordReceive = backRecordMapper.selectById(tOrder.getOrder_back_receive_id());
        }

        OrderBackRecord orderBackRecordSend = new OrderBackRecord();
        if (tOrder.getOrder_back_send_id() != null) {
            orderBackRecordSend = backRecordMapper.selectById(tOrder.getOrder_back_send_id());
        }

        double z_tf = 0, p_tf = 0, zz_f = 0;
        double z_ds = 0, p_ds = 0, zz_ds = 0;
        double z_zz = 0, p_zz = 0, zz_zz = 0;
        double z_ps = 0, p_ps = 0, zz_ps = 0;
        double z_sh = 0, p_sh = 0, zz_sh = 0;
        double z_by = 0, p_by = 0, zz_by = 0;
        double z_cc = 0, p_cc = 0, zz_cc = 0;
        double z_cf = 0, p_cf = 0, zz_cf = 0;
        double s_tf = 0, s_ccf = 0, s_by = 0; //上游
        double s_ds = 0, s_zz = 0, s_sh = 0; //上游
        double z_ld = 0, p_ld = 0, zz_ld = 0;
        for (TOrderFee tOrderFee : feeOrders) {
            switch (tOrderFee.getFeeId()) {
                case 1:
                    z_tf = z_tf + tOrderFee.getMoney();//提付 向下家收取 3
                    break;
                case 2:
                    z_ds = z_ds + tOrderFee.getMoney();//代收 向下家收取 3
                    break;
                case 3:
                    break;
                case 4:
                    break;
                case 5:
                    break;
                case 6:
                    z_zz = z_zz + tOrderFee.getMoney();//中转 向下家收取 3
                    break;
                case 20:
                    z_ps = z_ps + tOrderFee.getMoney();//配送 向下家收取 3
                    break;
                case 22:
                    s_tf = s_tf + tOrderFee.getMoney();//提付 向下家收取 2
                    break;
                case 23:
                    s_ds = s_ds + tOrderFee.getMoney();//代收 向下家收取 2
                    break;
                case 24:
                    s_zz = s_zz + tOrderFee.getMoney();//中转 向下家收取 2
                    break;
                case 25:
                    s_sh = s_sh + tOrderFee.getMoney();//送货 向下家收取 2
                    break;
                case 26:
                    s_by = s_by + tOrderFee.getMoney();//送货 向下家收取 2
                    break;
                case 27:
                    p_sh = p_sh + tOrderFee.getMoney();
                    break;
                case 28:
                    p_by = p_by + tOrderFee.getMoney();
                    break;
                case 29:
                    p_cc = p_cc + tOrderFee.getMoney();
                    break;
                case 30:
                    p_cf = p_cf + tOrderFee.getMoney();
                    break;
                case 32:
                    zz_f = zz_f + tOrderFee.getMoney();
                    break;
                case 33:
                    zz_ds = zz_ds + tOrderFee.getMoney();
                    break;
                case 34:
                    zz_zz = zz_zz + tOrderFee.getMoney();
                    break;
                case 35:
                    zz_by = zz_by + tOrderFee.getMoney();
                    break;
                case 36:
                    zz_sh = zz_sh + tOrderFee.getMoney();
                    break;
                case 37:
                    p_tf = p_tf + tOrderFee.getMoney();
                    break;
                case 38:
                    p_ds = p_ds + tOrderFee.getMoney();
                    break;
                case 39:
                    z_by = z_by + tOrderFee.getMoney();
                    break;
                case 40:
                    z_cc = z_cc + tOrderFee.getMoney();
                    break;
                case 41:
                    z_ld = z_ld + tOrderFee.getMoney();
                    break;
                case 42:
                    p_ld = p_ld + tOrderFee.getMoney();
                    break;
                case 43:
                    zz_ld = zz_ld + tOrderFee.getMoney();
                    break;
            }
        }

        List<Region> regionList = regionService.selectListByPid("1");//查找中国下级区域，即省份信息
        model.addAttribute("regionList", regionList);

        List<Region> city = regionService.selectListByPid(tOrder.getProvince_code() + "");
        model.addAttribute("cityList", city);
        List<Region> reg = regionService.selectListByPid(tOrder.getCity_code() + "");
        model.addAttribute("regList", reg);


        model.addAttribute("z_tf", z_tf);
        model.addAttribute("p_tf", p_tf);
        model.addAttribute("zz_f", zz_f);

        model.addAttribute("z_ds", z_ds);
        model.addAttribute("p_ds", p_ds);
        model.addAttribute("zz_ds", zz_ds);
        model.addAttribute("s_ds", s_ds);

        model.addAttribute("z_zz", z_zz);
        model.addAttribute("p_zz", p_zz);
        model.addAttribute("s_zz", s_zz);
        model.addAttribute("s_sh", s_sh);
        model.addAttribute("zz_zz", zz_zz);
        model.addAttribute("s_by", s_by);
        model.addAttribute("s_tf", s_tf);

        model.addAttribute("z_ps", z_ps);
        model.addAttribute("p_ps", p_ps);
        model.addAttribute("zz_ps", zz_ps);

        model.addAttribute("z_sh", z_sh);
        model.addAttribute("p_sh", p_sh);
        model.addAttribute("zz_sh", zz_sh);

        model.addAttribute("z_by", z_by);
        model.addAttribute("p_by", p_by);
        model.addAttribute("zz_by", zz_by);

        model.addAttribute("z_cc", z_cc);
        model.addAttribute("p_cc", p_cc);
        model.addAttribute("zz_cc", zz_cc);

        model.addAttribute("z_cf", z_cf);
        model.addAttribute("p_cf", p_cf);
        model.addAttribute("zz_cf", zz_cf);
        model.addAttribute("zz_ld", zz_ld);
        model.addAttribute("p_ld", p_ld);
        model.addAttribute("z_ld", z_ld);
        model.addAttribute("send_time", orderBackRecordSend.getSendTime() == null ? "" : DateUtil.formatDate(orderBackRecordSend.getSendTime(), ""));
        model.addAttribute("receive_time", orderBackRecordReceive.getReceiveTime() == null ? "" : DateUtil.formatDate(orderBackRecordReceive.getReceiveTime(), ""));
        model.addAttribute("orderBackRecordReceive", orderBackRecordReceive);
        model.addAttribute("orderBackRecordSend", orderBackRecordSend);
        model.addAttribute("createUser", userMapper.selectById(tOrder.getUserId()).getName());



        model.addAttribute("item", tOrder);
        if(StringUtils.isEmpty( tOrder.getNotice())|| tOrder.getNotice().equals("0")){
            model.addAttribute("notice","0");
        }else if( tOrder.getNotice().equals("1")){
            model.addAttribute("notice","1");
        }

        model.addAttribute("days", days);
        model.addAttribute("ccTime", DateUtil.formatDate(tOrder.getArrive_date(), ""));
        model.addAttribute("inTime", DateUtil.formatDate(tOrder.getOut_time(), ""));

        model.addAttribute("feeOrders", JSONArray.toJSONString(feeOrders));
        List<Map<String, Object>> list = tOrderService.getFee(ShiroKit.getUser().getDeptId() + "");


        model.addAttribute("list", list);
        model.addAttribute("tTrain", tTrain);
        if (tTrain1 == null) {
            model.addAttribute("tTrain1", new TTrain());
        } else {
            model.addAttribute("tTrain1", tTrain1);
        }

        model.addAttribute("feeOrders", feeOrders);

        model.addAttribute("title", tOrder.getMode() == 0 ? "自提" : "配送");
        if("0".equals(tOrder.getIsTransfer())&&tOrder.getMode() == 0){
            //单笔自提才能回退
            int i =monthbalanceDetailMapper.isSingleOut(tOrder.getId());
            if(i>0){
                model.addAttribute("backStatus", 0);
            }else{
                model.addAttribute("backStatus", 1);
            }
        }else{
            model.addAttribute("backStatus", 0);
        }
        LogObjectHolder.me().set(tOrder);
        return PREFIX + "summarylibrary.html";
    }


    //修改自提车费
    @RequestMapping(value = "/refOrderAndOrderFeeZT")
    @ResponseBody
    @Transactional
    public Object refOrderAndOrderFeeZT(TOrder order, String  send_Time,String  receive_time,String sender,String receiver,Integer send_id,Integer processMethod,Integer receive_id,String remark, SOrderFee sOrderFee) {


        Map param1=new HashMap();
        param1.put("dot_id",ShiroKit.getUser().getDotId());
        param1.put("nType", Const.NTYPE_04); // 类别 00 初始 01 配送 02 中转 03 自提  04 月结
        String bath_no = tDictMapper.getNumCode(param1);

        TOrder order1 = orderMapper.selectById(order.getId());
//        EntityWrapper<TOrderFee> entityWrapper = new EntityWrapper<>();
//        entityWrapper.where("order_id={0}", order.getId());
//        List<TOrderFee> feeOrders = itOrderFeeService.selectList(entityWrapper);
        List<Integer> ids = new ArrayList<>();
        ids.add(1);
        ids.add(2);
        ids.add(39);
        ids.add(40);//仓储
        ids.add(41);//落地费
        //上家的费用
        List<Integer> mIds = new ArrayList<>();
        mIds.add(22);
        mIds.add(23);
        mIds.add(24);
        mIds.add(25);
        mIds.add(26);
        List<TOrderFee> mOrderFees = orderFeeWarpper.selectOrderFee(order1.getId() + "", mIds);

        TMonthbalanceDetail sMonthbalanceDetail = new TMonthbalanceDetail();
        sMonthbalanceDetail.setCreater(ShiroKit.getUser().getId());
        sMonthbalanceDetail.setCreatetime(new Date());
        sMonthbalanceDetail.setDotId(order1.getDotId());
        sMonthbalanceDetail.setIncome(5);
        sMonthbalanceDetail.setIncomeId(order1.getId());
        sMonthbalanceDetail.setIncomeName(order1.getOriginal_num());
        sMonthbalanceDetail.setTraner(order1.getSendDeptName());
        if (sOrderFee.getIsPay().equals("0")) {
            sMonthbalanceDetail.setSettlementType(0);
            sMonthbalanceDetail.setStatus("1");

        } else {
            sMonthbalanceDetail.setSettlementType(1);
            sMonthbalanceDetail.setStatus("0");
        }
        if("0".equals(sOrderFee.getA1())){
            sMonthbalanceDetail.setStatus(OrderEnum.MONTHBALANCE_STATUS_SETTLED.getCode()+"");
        }
        sMonthbalanceDetail.setMoney(Double.valueOf(sOrderFee.getA1()));
        TBill sBill = new TBill();
        if (sOrderFee.getIsPay().equals("0")) {
            String billNum = "TT" + ShiroKit.getUser().getId() + "_" + DateUtil.getAllTimes();
            sBill.setDotId(order1.getDotId());
            sBill.setCreateTime(new Date());
            sBill.setBillNum(billNum);
            sBill.setEditUser(ShiroKit.getUser().getId());
            sBill.setCash(Float.valueOf(sOrderFee.getCashs()));
            sBill.setBank(Float.valueOf(sOrderFee.getBanks()));
            sBill.setAlipay(Float.valueOf(sOrderFee.getAlipays()));
            sBill.setEditUser(ShiroKit.getUser().getId());
            sBill.setWeixin(Float.valueOf(sOrderFee.getWeixins()));
            if(!("0".equals(sOrderFee.getCashs())&&"0".equals(sOrderFee.getBanks())&&"0".equals(sOrderFee.getAlipays())&&"0".equals(sOrderFee.getWeixins()))){
                billMapper.insert(sBill);
            }
        }

        sMonthbalanceDetail.setBillId(sBill.getId());
        sMonthbalanceDetail.setBalance_num(bath_no);
        monthbalanceDetailMapper.insert(sMonthbalanceDetail);
        if (order1.getFlow_status() > 0) {
            for (TOrderFee tOrderFee : mOrderFees) {//上家的费用都是新增
                tOrderFee.setId(null);
                if (tOrderFee.getFeeId() == 22 && tOrderFee.getMoney() != Double.valueOf(sOrderFee.getS_tf())) {

                    tOrderFee.setCreateUserId(ShiroKit.getUser().getId());
                    tOrderFee.setCreateTime(new Date());

                    tOrderFee.setBalance_id(sMonthbalanceDetail.getId());
                    if (Double.valueOf(sOrderFee.getS_tf()) - tOrderFee.getMoney() != 0) {
                        tOrderFee.setMoney(Double.valueOf(sOrderFee.getS_tf()) - tOrderFee.getMoney());
                        if (tOrderFee.getOrderId() == null) {
                            if (sOrderFee.getIsPay().equals("0")) {
                                tOrderFee.setSettlementType("0");
                            } else {
                                tOrderFee.setSettlementType("1");

                            }
                            if (order1.getFlow_status() == 4) {
                                tOrderFee.setAuditStatus(3);
                            } else {
                                tOrderFee.setAuditStatus(1);
                            }
                            tOrderFee.setDetpId(ShiroKit.getUser().getDeptId());
                            tOrderFee.setDotId(ShiroKit.getUser().getDotId());
                            tOrderFee.setOrderId(order.getId());
                            tOrderFee.setFeeIncome(0);
                        }
                        orderFeeWarpper.insert(tOrderFee);
                    }

                } else if (tOrderFee.getFeeId() == 23 && tOrderFee.getMoney() != Double.valueOf(sOrderFee.getS_ds())) {
                    tOrderFee.setCreateUserId(ShiroKit.getUser().getId());
                    tOrderFee.setBalance_id(sMonthbalanceDetail.getId());
                    tOrderFee.setCreateTime(new Date());
                    if (Double.valueOf(sOrderFee.getS_ds()) - tOrderFee.getMoney() != 0) {
                        tOrderFee.setMoney(Double.valueOf(sOrderFee.getS_ds()) - tOrderFee.getMoney());
                        if (tOrderFee.getOrderId() == null) {
                            if (sOrderFee.getIsPay().equals("0")) {
                                tOrderFee.setSettlementType("0");
                            } else {
                                tOrderFee.setSettlementType("1");

                            }
                            if (order1.getFlow_status() == 4) {
                                tOrderFee.setAuditStatus(3);
                            } else {
                                tOrderFee.setAuditStatus(1);
                            }
                            tOrderFee.setDetpId(ShiroKit.getUser().getDeptId());
                            tOrderFee.setDotId(ShiroKit.getUser().getDotId());
                            tOrderFee.setOrderId(order.getId());
                            tOrderFee.setFeeIncome(0);
                        }
                        orderFeeWarpper.insert(tOrderFee);
                    }


                } else if (tOrderFee.getFeeId() == 26 && tOrderFee.getMoney() != Double.valueOf(sOrderFee.getS_by())) {
                    tOrderFee.setCreateUserId(ShiroKit.getUser().getId());
                    tOrderFee.setBalance_id(sMonthbalanceDetail.getId());
                    tOrderFee.setCreateTime(new Date());
                    if (Double.valueOf(sOrderFee.getS_by()) - tOrderFee.getMoney() != 0) {
                        tOrderFee.setMoney(Double.valueOf(sOrderFee.getS_by()) - tOrderFee.getMoney());
                        if (tOrderFee.getOrderId() == null) {
                            if (sOrderFee.getIsPay().equals("0")) {
                                tOrderFee.setSettlementType("0");
                            } else {
                                tOrderFee.setSettlementType("1");

                            }
                            if (order1.getFlow_status() == 4) {
                                tOrderFee.setAuditStatus(3);
                            } else {
                                tOrderFee.setAuditStatus(1);
                            }
                            tOrderFee.setDetpId(ShiroKit.getUser().getDeptId());
                            tOrderFee.setDotId(ShiroKit.getUser().getDotId());
                            tOrderFee.setOrderId(order.getId());
                            tOrderFee.setFeeIncome(0);
                        }
                        orderFeeWarpper.insert(tOrderFee);
                    }

                } else if (tOrderFee.getFeeId() == 25 && tOrderFee.getMoney() != Double.valueOf(sOrderFee.getS_sh())) {
                    tOrderFee.setCreateUserId(ShiroKit.getUser().getId());
                    tOrderFee.setBalance_id(sMonthbalanceDetail.getId());
                    tOrderFee.setCreateTime(new Date());
                    if (Double.valueOf(sOrderFee.getS_sh()) - tOrderFee.getMoney() != 0) {
                        tOrderFee.setMoney(Double.valueOf(sOrderFee.getS_sh()) - tOrderFee.getMoney());
                        if (tOrderFee.getOrderId() == null) {
                            if (sOrderFee.getIsPay().equals("0")) {
                                tOrderFee.setSettlementType("0");
                            } else {
                                tOrderFee.setSettlementType("1");

                            }
                            if (order1.getFlow_status() == 4) {
                                tOrderFee.setAuditStatus(3);
                            } else {
                                tOrderFee.setAuditStatus(1);
                            }
                            tOrderFee.setDetpId(ShiroKit.getUser().getDeptId());
                            tOrderFee.setDotId(ShiroKit.getUser().getDotId());
                            tOrderFee.setOrderId(order.getId());
                            tOrderFee.setFeeIncome(0);
                        }
                        orderFeeWarpper.insert(tOrderFee);
                    }

                } else if (tOrderFee.getFeeId() == 24 && tOrderFee.getMoney() != Double.valueOf(sOrderFee.getS_zz())) {
                    tOrderFee.setCreateUserId(ShiroKit.getUser().getId());
                    tOrderFee.setBalance_id(sMonthbalanceDetail.getId());
                    tOrderFee.setCreateTime(new Date());
                    if (Double.valueOf(sOrderFee.getS_zz()) - tOrderFee.getMoney() != 0) {
                        tOrderFee.setMoney(Double.valueOf(sOrderFee.getS_zz()) - tOrderFee.getMoney());
                        if (tOrderFee.getOrderId() == null) {
                            if (sOrderFee.getIsPay().equals("0")) {
                                tOrderFee.setSettlementType("0");
                            } else {
                                tOrderFee.setSettlementType("1");

                            }
                            if (order1.getFlow_status() == 4) {
                                tOrderFee.setAuditStatus(3);
                            } else {
                                tOrderFee.setAuditStatus(1);
                            }
                            tOrderFee.setDetpId(ShiroKit.getUser().getDeptId());
                            tOrderFee.setDotId(ShiroKit.getUser().getDotId());
                            tOrderFee.setOrderId(order.getId());
                            tOrderFee.setFeeIncome(0);
                        }

                        orderFeeWarpper.insert(tOrderFee);
                    }
                }

//                if (sOrderFee.getIsPay().equals("0") && tOrderFee.getId() != null) {
//                    Map param = new HashMap();
//                    param.put("order_fee_id", tOrderFee.getId() + "");
//                    param.put("order_id", tOrderFee.getOrderId() + "");
//                    param.put("bill_id", sBill.getId());
//                    tTrainMapper.addFeeBill(param);
//                }
            }
        } else {


            for (TOrderFee tOrderFee : mOrderFees) {
                if (tOrderFee.getFeeId() == 22 && tOrderFee.getMoney() != Double.valueOf(sOrderFee.getS_tf())) {
                    if (tOrderFee.getOrderId() == null) {
                        tOrderFee.setCreateUserId(ShiroKit.getUser().getId());
                        tOrderFee.setCreateTime(new Date());
                        tOrderFee.setMoney(Double.valueOf(sOrderFee.getS_tf()));
                        tOrderFee.setAuditStatus(0);//未结算
                        tOrderFee.setDetpId(ShiroKit.getUser().getDeptId());
                        tOrderFee.setDotId(ShiroKit.getUser().getDotId());
                        tOrderFee.setOrderId(order.getId());
                        tOrderFee.setFeeIncome(0);
                        orderFeeWarpper.insert(tOrderFee);
                    } else {
                        tOrderFee.setCreateUserId(ShiroKit.getUser().getId());
                        tOrderFee.setCreateTime(new Date());
                        tOrderFee.setMoney(Double.valueOf(sOrderFee.getS_tf()));
                        orderFeeWarpper.updateById(tOrderFee);
                    }
                } else if (tOrderFee.getFeeId() == 23 && tOrderFee.getMoney() != Double.valueOf(sOrderFee.getS_ds())) {
                    if (tOrderFee.getOrderId() == null) {
                        tOrderFee.setCreateUserId(ShiroKit.getUser().getId());
                        tOrderFee.setCreateTime(new Date());
                        tOrderFee.setMoney(Double.valueOf(sOrderFee.getS_ds()));
                        tOrderFee.setAuditStatus(0);//未结算
                        tOrderFee.setDetpId(ShiroKit.getUser().getDeptId());
                        tOrderFee.setDotId(ShiroKit.getUser().getDotId());
                        tOrderFee.setOrderId(order.getId());
                        tOrderFee.setFeeIncome(0);
                        orderFeeWarpper.insert(tOrderFee);
                    } else {
                        tOrderFee.setCreateUserId(ShiroKit.getUser().getId());
                        tOrderFee.setCreateTime(new Date());
                        tOrderFee.setMoney(Double.valueOf(sOrderFee.getS_ds()));
                        orderFeeWarpper.updateById(tOrderFee);
                    }
                } else if (tOrderFee.getFeeId() == 26 && tOrderFee.getMoney() != Double.valueOf(sOrderFee.getS_by())) {
                    if (tOrderFee.getOrderId() == null) {
                        tOrderFee.setCreateUserId(ShiroKit.getUser().getId());
                        tOrderFee.setCreateTime(new Date());
                        tOrderFee.setMoney(Double.valueOf(sOrderFee.getS_ds()));
                        tOrderFee.setAuditStatus(0);//未结算
                        tOrderFee.setDetpId(ShiroKit.getUser().getDeptId());
                        tOrderFee.setDotId(ShiroKit.getUser().getDotId());
                        tOrderFee.setOrderId(order.getId());
                        tOrderFee.setFeeIncome(0);
                        orderFeeWarpper.insert(tOrderFee);
                    } else {
                        tOrderFee.setCreateUserId(ShiroKit.getUser().getId());
                        tOrderFee.setCreateTime(new Date());
                        tOrderFee.setMoney(Double.valueOf(sOrderFee.getS_by()));
                        orderFeeWarpper.updateById(tOrderFee);
                    }
                } else if (tOrderFee.getFeeId() == 25 && tOrderFee.getMoney() != Double.valueOf(sOrderFee.getS_sh())) {
                    if (tOrderFee.getOrderId() == null) {
                        tOrderFee.setCreateUserId(ShiroKit.getUser().getId());
                        tOrderFee.setCreateTime(new Date());
                        tOrderFee.setMoney(Double.valueOf(sOrderFee.getS_sh()));
                        tOrderFee.setAuditStatus(0);//未结算
                        tOrderFee.setDetpId(ShiroKit.getUser().getDeptId());
                        tOrderFee.setDotId(ShiroKit.getUser().getDotId());
                        tOrderFee.setOrderId(order.getId());
                        tOrderFee.setFeeIncome(0);
                        orderFeeWarpper.insert(tOrderFee);
                    } else {
                        tOrderFee.setCreateUserId(ShiroKit.getUser().getId());
                        tOrderFee.setCreateTime(new Date());
                        tOrderFee.setMoney(Double.valueOf(sOrderFee.getS_sh()));
                        orderFeeWarpper.updateById(tOrderFee);
                    }
                } else if (tOrderFee.getFeeId() == 24 && tOrderFee.getMoney() != Double.valueOf(sOrderFee.getS_zz())) {
                    if (tOrderFee.getOrderId() == null) {
                        tOrderFee.setCreateUserId(ShiroKit.getUser().getId());
                        tOrderFee.setCreateTime(new Date());
                        tOrderFee.setMoney(Double.valueOf(sOrderFee.getS_zz()));
                        tOrderFee.setAuditStatus(0);//未结算
                        tOrderFee.setDetpId(ShiroKit.getUser().getDeptId());
                        tOrderFee.setDotId(ShiroKit.getUser().getDotId());
                        tOrderFee.setOrderId(order.getId());
                        tOrderFee.setFeeIncome(0);
                        orderFeeWarpper.insert(tOrderFee);
                    } else {
                        tOrderFee.setCreateUserId(ShiroKit.getUser().getId());
                        tOrderFee.setCreateTime(new Date());
                        tOrderFee.setMoney(Double.valueOf(sOrderFee.getS_zz()));
                        orderFeeWarpper.updateById(tOrderFee);
                    }
                }
            }

        }


        List<TOrderFee> orderFees = orderFeeWarpper.selectOrderFee(order1.getId() + "", ids);
        // int stu=feeOrders.get(0).getAuditStatus();
        if (order1.getFlow_status() >= 3) {
            TMonthbalanceDetail tMonthbalanceDetail = new TMonthbalanceDetail();
            tMonthbalanceDetail.setCreater(ShiroKit.getUser().getId());
            tMonthbalanceDetail.setCreatetime(new Date());
            tMonthbalanceDetail.setDotId(order1.getDotId());
            tMonthbalanceDetail.setIncome(5);
            tMonthbalanceDetail.setIncomeId(order1.getId());
            tMonthbalanceDetail.setIncomeName(order1.getOriginal_num());
            tMonthbalanceDetail.setSettlementType(0);
            tMonthbalanceDetail.setStatus("1");
            tMonthbalanceDetail.setMoney(Double.valueOf(sOrderFee.getCashz()) + Double.valueOf(sOrderFee.getWeixinz())
                    + Double.valueOf(sOrderFee.getBanksz()) + Double.valueOf(sOrderFee.getAlipayz()));



            Map param2=new HashMap();
            param2.put("dot_id",ShiroKit.getUser().getDotId());
            param2.put("nType", Const.NTYPE_04); // 类别 00 初始 01 配送 02 中转 03 自提  04 月结
            String balance_num = tDictMapper.getNumCode(param2);

            String billNum = "TT" + ShiroKit.getUser().getId() + "_" + DateUtil.getAllTimes();
            TBill bill = new TBill();
            bill.setDotId(order1.getDotId());
            bill.setCreateTime(new Date());
            bill.setEditUser(ShiroKit.getUser().getId());
            bill.setBillNum(billNum);
            bill.setCash(Float.valueOf(sOrderFee.getCashz()));
            bill.setBank(Float.valueOf(sOrderFee.getBanksz()));
            bill.setAlipay(Float.valueOf(sOrderFee.getAlipayz()));
            bill.setWeixin(Float.valueOf(sOrderFee.getWeixinz()));

            if(!("0".equals(sOrderFee.getCashz())&&"0".equals(sOrderFee.getBanksz())&&"0".equals(sOrderFee.getAlipayz())&&"0".equals(sOrderFee.getWeixinz()))){
                billMapper.insert(bill);
            }

            tMonthbalanceDetail.setBillId(bill.getId());
            tMonthbalanceDetail.setTraner(order.getConsignee());
            tMonthbalanceDetail.setBalance_num(balance_num);
            if("0".equals(sOrderFee.getCashz())&&"0".equals(sOrderFee.getBanksz())&&"0".equals(sOrderFee.getAlipayz())&&"0".equals(sOrderFee.getWeixinz())) {
                tMonthbalanceDetail.setStatus(OrderEnum.MONTHBALANCE_STATUS_SETTLED.getCode()+"");
            }
            monthbalanceDetailMapper.insert(tMonthbalanceDetail);

            for (TOrderFee tOrderFee : orderFees) {
                tOrderFee.setId(null);

                if (tOrderFee.getFeeId() == 1 && tOrderFee.getMoney() != Double.valueOf(sOrderFee.getZ_tf())) {
                    if (tOrderFee.getOrderId() == null) {
                        tOrderFee.setSettlementType("0");
                        if (order1.getFlow_status() == 4) {
                            tOrderFee.setAuditStatus(3);
                        } else {
                            tOrderFee.setAuditStatus(1);
                        }
                        tOrderFee.setDetpId(ShiroKit.getUser().getDeptId());
                        tOrderFee.setDotId(ShiroKit.getUser().getDotId());
                        tOrderFee.setOrderId(order.getId());
                        tOrderFee.setFeeIncome(0);
                    }
                    tOrderFee.setCreateUserId(ShiroKit.getUser().getId());
                    tOrderFee.setCreateTime(new Date());
                    tOrderFee.setMoney(Double.valueOf(sOrderFee.getZ_tf()) - tOrderFee.getMoney());
                    tOrderFee.setBalance_id(tMonthbalanceDetail.getId());
                    orderFeeWarpper.insert(tOrderFee);


                } else if (tOrderFee.getFeeId() == 41 && tOrderFee.getMoney() != Double.valueOf(sOrderFee.getZ_ld())) {
                    if (tOrderFee.getOrderId() == null) {
                        tOrderFee.setSettlementType("0");
                        if (order1.getFlow_status() == 4) {
                            tOrderFee.setAuditStatus(3);
                        } else {
                            tOrderFee.setAuditStatus(1);
                        }
                        tOrderFee.setDetpId(ShiroKit.getUser().getDeptId());
                        tOrderFee.setDotId(ShiroKit.getUser().getDotId());
                        tOrderFee.setOrderId(order.getId());
                        tOrderFee.setFeeIncome(0);
                    }
                    tOrderFee.setCreateUserId(ShiroKit.getUser().getId());
                    tOrderFee.setCreateTime(new Date());
                    tOrderFee.setMoney(Double.valueOf(sOrderFee.getZ_ld()) - tOrderFee.getMoney());
                    tOrderFee.setBalance_id(tMonthbalanceDetail.getId());
                    orderFeeWarpper.insert(tOrderFee);


                } else if (tOrderFee.getFeeId() == 2 && tOrderFee.getMoney() != Double.valueOf(sOrderFee.getZ_ds())) {
                    if (tOrderFee.getOrderId() == null) {
                        tOrderFee.setSettlementType("0");
                        if (order1.getFlow_status() == 4) {
                            tOrderFee.setAuditStatus(3);
                        } else {
                            tOrderFee.setAuditStatus(1);
                        }
                        tOrderFee.setDetpId(ShiroKit.getUser().getDeptId());
                        tOrderFee.setDotId(ShiroKit.getUser().getDotId());
                        tOrderFee.setOrderId(order.getId());
                        tOrderFee.setFeeIncome(0);
                    }
                    tOrderFee.setCreateUserId(ShiroKit.getUser().getId());
                    tOrderFee.setBalance_id(tMonthbalanceDetail.getId());
                    tOrderFee.setCreateTime(new Date());
                    tOrderFee.setMoney(Double.valueOf(sOrderFee.getZ_ds()) - tOrderFee.getMoney());
                    orderFeeWarpper.insert(tOrderFee);
//                    Map param =new HashMap();
//                    param.put("order_fee_id",tOrderFee.getId()+"");
//                    param.put("order_id",tOrderFee.getOrderId()+"");
//                    param.put("bill_id",bill.getId());
//                    tTrainMapper.addFeeBill(param);

                } else if (tOrderFee.getFeeId() == 39 && tOrderFee.getMoney() != Double.valueOf(sOrderFee.getZ_by())) {
                    if (tOrderFee.getOrderId() == null) {
                        tOrderFee.setSettlementType("0");
                        if (order1.getFlow_status() == 4) {
                            tOrderFee.setAuditStatus(3);
                        } else {
                            tOrderFee.setAuditStatus(1);
                        }
                        tOrderFee.setDetpId(ShiroKit.getUser().getDeptId());
                        tOrderFee.setDotId(ShiroKit.getUser().getDotId());
                        tOrderFee.setOrderId(order.getId());
                        tOrderFee.setFeeIncome(0);
                    }
                    tOrderFee.setCreateUserId(ShiroKit.getUser().getId());
                    tOrderFee.setBalance_id(tMonthbalanceDetail.getId());
                    tOrderFee.setCreateTime(new Date());
                    tOrderFee.setMoney(Double.valueOf(sOrderFee.getZ_by()) - tOrderFee.getMoney());
                    orderFeeWarpper.insert(tOrderFee);

//                    Map param =new HashMap();
//                    param.put("order_fee_id",tOrderFee.getId()+"");
//                    param.put("order_id",tOrderFee.getOrderId()+"");
//                    param.put("bill_id",bill.getId());
//                    tTrainMapper.addFeeBill(param);

                } else if (tOrderFee.getFeeId() == 40 && tOrderFee.getMoney() != Double.valueOf(sOrderFee.getZ_cc())) {
                    if (tOrderFee.getOrderId() == null) {
                        tOrderFee.setSettlementType("0");
                        if (order1.getFlow_status() == 4) {
                            tOrderFee.setAuditStatus(3);
                        } else {
                            tOrderFee.setAuditStatus(1);
                        }
                        tOrderFee.setDetpId(ShiroKit.getUser().getDeptId());
                        tOrderFee.setDotId(ShiroKit.getUser().getDotId());
                        tOrderFee.setOrderId(order.getId());
                        tOrderFee.setFeeIncome(0);
                    }
                    tOrderFee.setCreateUserId(ShiroKit.getUser().getId());
                    tOrderFee.setBalance_id(tMonthbalanceDetail.getId());
                    tOrderFee.setCreateTime(new Date());
                    tOrderFee.setMoney(Double.valueOf(sOrderFee.getZ_cc()) - tOrderFee.getMoney());
                    orderFeeWarpper.insert(tOrderFee);

//                    Map param =new HashMap();
//                    param.put("order_fee_id",tOrderFee.getId()+"");
//                    param.put("order_id",tOrderFee.getOrderId()+"");
//                    param.put("bill_id",bill.getId());
//                    tTrainMapper.addFeeBill(param);
                }
//                Map param = new HashMap();
//                param.put("order_fee_id", tOrderFee.getId() + "");
//                param.put("order_id", tOrderFee.getOrderId() + "");
//                param.put("bill_id", bill.getId());
//                tTrainMapper.addFeeBill(param);

            }
        } else {//更新数据
            for (TOrderFee tOrderFee : orderFees) {
                if (tOrderFee.getFeeId() == 1 && tOrderFee.getMoney() != Double.valueOf(sOrderFee.getZ_tf())) {
                    if (tOrderFee.getOrderId() == null) {
                        tOrderFee.setCreateUserId(ShiroKit.getUser().getId());
                        tOrderFee.setCreateTime(new Date());
                        tOrderFee.setMoney(Double.valueOf(sOrderFee.getZ_tf()));
                        tOrderFee.setAuditStatus(0);//未结算
                        tOrderFee.setDetpId(ShiroKit.getUser().getDeptId());
                        tOrderFee.setDotId(ShiroKit.getUser().getDotId());
                        tOrderFee.setOrderId(order.getId());
                        tOrderFee.setFeeIncome(0);
                        orderFeeWarpper.insert(tOrderFee);
                    } else {
                        tOrderFee.setCreateUserId(ShiroKit.getUser().getId());
                        tOrderFee.setCreateTime(new Date());
                        tOrderFee.setMoney(Double.valueOf(sOrderFee.getZ_tf()));
                        orderFeeWarpper.updateById(tOrderFee);
                    }
                } else if (tOrderFee.getFeeId() == 41 && tOrderFee.getMoney() != Double.valueOf(sOrderFee.getZ_ld())) {
                    if (tOrderFee.getOrderId() == null) {
                        tOrderFee.setCreateUserId(ShiroKit.getUser().getId());
                        tOrderFee.setCreateTime(new Date());
                        tOrderFee.setMoney(Double.valueOf(sOrderFee.getZ_ld()));
                        tOrderFee.setAuditStatus(0);//未结算
                        tOrderFee.setDetpId(ShiroKit.getUser().getDeptId());
                        tOrderFee.setDotId(ShiroKit.getUser().getDotId());
                        tOrderFee.setOrderId(order.getId());
                        tOrderFee.setFeeIncome(0);
                        orderFeeWarpper.insert(tOrderFee);
                    } else {
                        tOrderFee.setCreateUserId(ShiroKit.getUser().getId());
                        tOrderFee.setCreateTime(new Date());
                        tOrderFee.setMoney(Double.valueOf(sOrderFee.getZ_ld()));
                        orderFeeWarpper.updateById(tOrderFee);
                    }
                } else if (tOrderFee.getFeeId() == 2 && tOrderFee.getMoney() != Double.valueOf(sOrderFee.getZ_ds())) {
                    if (tOrderFee.getOrderId() == null) {
                        tOrderFee.setCreateUserId(ShiroKit.getUser().getId());
                        tOrderFee.setCreateTime(new Date());
                        tOrderFee.setMoney(Double.valueOf(sOrderFee.getZ_ds()));
                        tOrderFee.setAuditStatus(0);//未结算
                        tOrderFee.setDetpId(ShiroKit.getUser().getDeptId());
                        tOrderFee.setDotId(ShiroKit.getUser().getDotId());
                        tOrderFee.setOrderId(order.getId());
                        tOrderFee.setFeeIncome(0);
                        orderFeeWarpper.insert(tOrderFee);
                    } else {
                        tOrderFee.setCreateUserId(ShiroKit.getUser().getId());
                        tOrderFee.setCreateTime(new Date());
                        tOrderFee.setMoney(Double.valueOf(sOrderFee.getZ_ds()));
                        orderFeeWarpper.updateById(tOrderFee);
                    }
                } else if (tOrderFee.getFeeId() == 39 && tOrderFee.getMoney() != Double.valueOf(sOrderFee.getZ_by())) {
                    if (tOrderFee.getOrderId() == null) {
                        tOrderFee.setCreateUserId(ShiroKit.getUser().getId());
                        tOrderFee.setCreateTime(new Date());
                        tOrderFee.setMoney(Double.valueOf(sOrderFee.getZ_by()));
                        tOrderFee.setAuditStatus(0);//未结算
                        tOrderFee.setDetpId(ShiroKit.getUser().getDeptId());
                        tOrderFee.setDotId(ShiroKit.getUser().getDotId());
                        tOrderFee.setOrderId(order.getId());
                        tOrderFee.setFeeIncome(0);
                        orderFeeWarpper.insert(tOrderFee);
                    } else {
                        tOrderFee.setCreateUserId(ShiroKit.getUser().getId());
                        tOrderFee.setCreateTime(new Date());
                        tOrderFee.setMoney(Double.valueOf(sOrderFee.getZ_by()));
                        orderFeeWarpper.updateById(tOrderFee);
                    }
                } else if (tOrderFee.getFeeId() == 40 && tOrderFee.getMoney() != Double.valueOf(sOrderFee.getZ_cc())) {
                    if (tOrderFee.getOrderId() == null) {
                        tOrderFee.setCreateUserId(ShiroKit.getUser().getId());
                        tOrderFee.setCreateTime(new Date());
                        tOrderFee.setMoney(Double.valueOf(sOrderFee.getZ_cc()));
                        tOrderFee.setAuditStatus(0);//未结算
                        tOrderFee.setDetpId(ShiroKit.getUser().getDeptId());
                        tOrderFee.setDotId(ShiroKit.getUser().getDotId());
                        tOrderFee.setOrderId(order.getId());
                        tOrderFee.setFeeIncome(0);
                        orderFeeWarpper.insert(tOrderFee);
                    } else {
                        tOrderFee.setCreateUserId(ShiroKit.getUser().getId());
                        tOrderFee.setCreateTime(new Date());
                        tOrderFee.setMoney(Double.valueOf(sOrderFee.getZ_cc()));
                        orderFeeWarpper.updateById(tOrderFee);
                    }
                }
            }

        }
        if(receive_id!=null){
            OrderBackRecord orderReceive=new OrderBackRecord();
            orderReceive.setId(receive_id);
            if(receive_time!=null&&!receive_time.isEmpty()){
                orderReceive.setReceiveTime(DateUtil.parseDate(receive_time));
            }
            orderReceive.setRemark(remark);
            orderReceive.setReceiver(receiver);
            orderReceive.setEdit_time(new Date());
            orderReceive.setEdit_user(ShiroKit.getUser().getId());
            backRecordMapper.updateById(orderReceive);
        }
        if(send_id!=null){
            OrderBackRecord orderSend=new OrderBackRecord();
            orderSend.setId(send_id);
            if(send_Time!=null&&!send_Time.isEmpty()){
                orderSend.setSendTime(DateUtil.parseDate(send_Time));
            }
            orderSend.setRemark(remark);
            orderSend.setReceiver(sender);
            orderSend.setProcessMethod(processMethod);
            orderSend.setEdit_time(new Date());
            orderSend.setEdit_user(ShiroKit.getUser().getId());
            backRecordMapper.updateById(orderSend);
        }

        order.setEdit_user(ShiroKit.getUser().getId());
        order.setEdit_time(new Date());
        orderMapper.updateById(order);

        return super.SUCCESS_TIP;
    }


    //修改配送车费
    @RequestMapping(value = "/refOrderAndOrderFeePS")
    @ResponseBody
    @Transactional
    public Object refOrderAndOrderFeePS(TOrder order, String  send_Time,String  receive_time,String sender,String receiver,Integer send_id,Integer processMethod,Integer receive_id,String remark , SOrderFee sOrderFee) {
        TOrder order1 = orderMapper.selectById(order.getId());
        List<Integer> ids = new ArrayList<>();
        ids.add(27); ids.add(28); ids.add(29);ids.add(30);ids.add(37); ids.add(38); ids.add(42);//落地费
        //上家的费用
        List<Integer> mIds = new ArrayList<>();
        mIds.add(22);   mIds.add(23);mIds.add(24);mIds.add(25);mIds.add(26);
        List<TOrderFee> mOrderFees = orderFeeWarpper.selectOrderFee(order1.getId() + "", mIds);


        Map param3=new HashMap();
        param3.put("dot_id",ShiroKit.getUser().getDotId());
        param3.put("nType", Const.NTYPE_04); // 类别 00 初始 01 配送 02 中转 03 自提  04 月结
//        String bath_no = tDictMapper.getNumCode(param3);

        TMonthbalanceDetail sMonthbalanceDetail = new TMonthbalanceDetail();
        sMonthbalanceDetail.setCreater(ShiroKit.getUser().getId());
        sMonthbalanceDetail.setCreatetime(new Date());
        sMonthbalanceDetail.setDotId(order1.getDotId());
        sMonthbalanceDetail.setIncome(5);
        sMonthbalanceDetail.setIncomeId(order1.getId());
        sMonthbalanceDetail.setIncomeName(order1.getOriginal_num());
        sMonthbalanceDetail.setTraner(order1.getSendDeptName());
        if (sOrderFee.getIsPay().equals("0")) {
            sMonthbalanceDetail.setSettlementType(0);
            sMonthbalanceDetail.setStatus("1");
        } else {
            sMonthbalanceDetail.setSettlementType(1);
            sMonthbalanceDetail.setStatus("0");
        }
        sMonthbalanceDetail.setMoney(Double.valueOf(sOrderFee.getA1()));
        TBill sBill = new TBill();
        if (sOrderFee.getIsPay().equals("0")) {
            String billNum = "TT" + ShiroKit.getUser().getId() + "_" + DateUtil.getAllTimes();
            sBill.setDotId(order1.getDotId());
            sBill.setCreateTime(new Date());
            sBill.setEditUser(ShiroKit.getUser().getId());
            sBill.setBillNum(billNum);
            sBill.setCash(Float.valueOf(sOrderFee.getCashs()));
            sBill.setBank(Float.valueOf(sOrderFee.getBanks()));
            sBill.setAlipay(Float.valueOf(sOrderFee.getAlipays()));
            sBill.setWeixin(Float.valueOf(sOrderFee.getWeixins()));
            sBill.setEditUser(ShiroKit.getUser().getId());
            if(!("0".equals(sOrderFee.getCashs())&&"0".equals(sOrderFee.getBanks())&&"0".equals(sOrderFee.getAlipays())&&"0".equals(sOrderFee.getWeixins()))){
                billMapper.insert(sBill);
            }
        }
        sMonthbalanceDetail.setBillId(sBill.getId());
        if("0".equals(sOrderFee.getA1())){
            sMonthbalanceDetail.setStatus(OrderEnum.MONTHBALANCE_STATUS_SETTLED.getCode()+"");
        }
        sMonthbalanceDetail.setBalance_num(tDictMapper.getNumCode(param3));
        monthbalanceDetailMapper.insert(sMonthbalanceDetail);
        if (order1.getFlow_status() > 0) {
            for (TOrderFee tOrderFee : mOrderFees) {//上家的费用都是新增
                tOrderFee.setId(null);
                if (tOrderFee.getFeeId() == 22 && tOrderFee.getMoney() != Double.valueOf(sOrderFee.getS_tf())) {
                    tOrderFee.setCreateUserId(ShiroKit.getUser().getId());
                    tOrderFee.setCreateTime(new Date());

                    tOrderFee.setBalance_id(sMonthbalanceDetail.getId());
                    if (Double.valueOf(sOrderFee.getS_tf()) - tOrderFee.getMoney() != 0) {
                        tOrderFee.setMoney(Double.valueOf(sOrderFee.getS_tf()) - tOrderFee.getMoney());
                        if (tOrderFee.getOrderId() == null) {
                            if (sOrderFee.getIsPay().equals("0")) {//现结
                                tOrderFee.setSettlementType("0");
                            } else {
                                tOrderFee.setSettlementType("1");

                            }
                            if (order1.getFlow_status() == 4) {
                                tOrderFee.setAuditStatus(3);
                            } else {
                                tOrderFee.setAuditStatus(1);
                            }
                            tOrderFee.setDetpId(ShiroKit.getUser().getDeptId());
                            tOrderFee.setDotId(ShiroKit.getUser().getDotId());
                            tOrderFee.setOrderId(order.getId());
                            tOrderFee.setFeeIncome(0);
                        }
                        orderFeeWarpper.insert(tOrderFee);
                    }

                } else if (tOrderFee.getFeeId() == 23 && tOrderFee.getMoney() != Double.valueOf(sOrderFee.getS_ds())) {
                    tOrderFee.setCreateUserId(ShiroKit.getUser().getId());
                    tOrderFee.setBalance_id(sMonthbalanceDetail.getId());
                    tOrderFee.setCreateTime(new Date());
                    if (Double.valueOf(sOrderFee.getS_ds()) - tOrderFee.getMoney() != 0) {
                        tOrderFee.setMoney(Double.valueOf(sOrderFee.getS_ds()) - tOrderFee.getMoney());
                        if (tOrderFee.getOrderId() == null) {
                            if (sOrderFee.getIsPay().equals("0")) {//现结
                                tOrderFee.setSettlementType("0");
                            } else {
                                tOrderFee.setSettlementType("1");

                            }
                            if (order1.getFlow_status() == 4) {
                                tOrderFee.setAuditStatus(3);
                            } else {
                                tOrderFee.setAuditStatus(1);
                            }
                            tOrderFee.setDetpId(ShiroKit.getUser().getDeptId());
                            tOrderFee.setDotId(ShiroKit.getUser().getDotId());
                            tOrderFee.setOrderId(order.getId());
                            tOrderFee.setFeeIncome(0);
                        }
                        orderFeeWarpper.insert(tOrderFee);
                    }


                } else if (tOrderFee.getFeeId() == 26 && tOrderFee.getMoney() != Double.valueOf(sOrderFee.getS_by())) {
                    tOrderFee.setCreateUserId(ShiroKit.getUser().getId());
                    tOrderFee.setBalance_id(sMonthbalanceDetail.getId());
                    tOrderFee.setCreateTime(new Date());
                    if (Double.valueOf(sOrderFee.getS_by()) - tOrderFee.getMoney() != 0) {
                        tOrderFee.setMoney(Double.valueOf(sOrderFee.getS_by()) - tOrderFee.getMoney());
                        if (tOrderFee.getOrderId() == null) {
                            if (sOrderFee.getIsPay().equals("0")) {//现结
                                tOrderFee.setSettlementType("0");
                            } else {
                                tOrderFee.setSettlementType("1");

                            }
                            if (order1.getFlow_status() == 4) {
                                tOrderFee.setAuditStatus(3);
                            } else {
                                tOrderFee.setAuditStatus(1);
                            }
                            tOrderFee.setDetpId(ShiroKit.getUser().getDeptId());
                            tOrderFee.setDotId(ShiroKit.getUser().getDotId());
                            tOrderFee.setOrderId(order.getId());
                            tOrderFee.setFeeIncome(0);
                        }
                        orderFeeWarpper.insert(tOrderFee);
                    }

                } else if (tOrderFee.getFeeId() == 25 && tOrderFee.getMoney() != Double.valueOf(sOrderFee.getS_sh())) {
                    tOrderFee.setCreateUserId(ShiroKit.getUser().getId());
                    tOrderFee.setBalance_id(sMonthbalanceDetail.getId());
                    tOrderFee.setCreateTime(new Date());
                    if (Double.valueOf(sOrderFee.getS_sh()) - tOrderFee.getMoney() != 0) {
                        tOrderFee.setMoney(Double.valueOf(sOrderFee.getS_sh()) - tOrderFee.getMoney());
                        if (tOrderFee.getOrderId() == null) {
                            if (sOrderFee.getIsPay().equals("0")) {//现结
                                tOrderFee.setSettlementType("0");
                            } else {
                                tOrderFee.setSettlementType("1");

                            }
                            if (order1.getFlow_status() == 4) {
                                tOrderFee.setAuditStatus(3);
                            } else {
                                tOrderFee.setAuditStatus(1);
                            }
                            tOrderFee.setDetpId(ShiroKit.getUser().getDeptId());
                            tOrderFee.setDotId(ShiroKit.getUser().getDotId());
                            tOrderFee.setOrderId(order.getId());
                            tOrderFee.setFeeIncome(0);
                        }
                        orderFeeWarpper.insert(tOrderFee);
                    }

                } else if (tOrderFee.getFeeId() == 24 && tOrderFee.getMoney() != Double.valueOf(sOrderFee.getS_zz())) {
                    tOrderFee.setCreateUserId(ShiroKit.getUser().getId());
                    tOrderFee.setBalance_id(sMonthbalanceDetail.getId());
                    tOrderFee.setCreateTime(new Date());
                    if (Double.valueOf(sOrderFee.getS_zz()) - tOrderFee.getMoney() != 0) {
                        tOrderFee.setMoney(Double.valueOf(sOrderFee.getS_zz()) - tOrderFee.getMoney());
                        if (tOrderFee.getOrderId() == null) {
                            if (sOrderFee.getIsPay().equals("0")) {//现结
                                tOrderFee.setSettlementType("0");
                            } else {
                                tOrderFee.setSettlementType("1");

                            }
                            if (order1.getFlow_status() == 4) {
                                tOrderFee.setAuditStatus(3);
                            } else {
                                tOrderFee.setAuditStatus(1);
                            }
                            tOrderFee.setDetpId(ShiroKit.getUser().getDeptId());
                            tOrderFee.setDotId(ShiroKit.getUser().getDotId());
                            tOrderFee.setOrderId(order.getId());
                            tOrderFee.setFeeIncome(0);
                        }
                        orderFeeWarpper.insert(tOrderFee);
                    }
                }
//
//                if (sOrderFee.getIsPay().equals("0") && tOrderFee.getId() != null) {
//                    Map param = new HashMap();
//                    param.put("order_fee_id", tOrderFee.getId() + "");
//                    param.put("order_id", tOrderFee.getOrderId() + "");
//                    param.put("bill_id", sBill.getId());
//                    tTrainMapper.addFeeBill(param);
//                }
            }
        } else {

            for (TOrderFee tOrderFee : mOrderFees) {
                if (tOrderFee.getFeeId() == 22 && tOrderFee.getMoney() != Double.valueOf(sOrderFee.getS_tf())) {
                    if (tOrderFee.getOrderId() == null) {
                        tOrderFee.setCreateUserId(ShiroKit.getUser().getId());
                        tOrderFee.setCreateTime(new Date());
                        tOrderFee.setMoney(Double.valueOf(sOrderFee.getP_tf()));
                        tOrderFee.setAuditStatus(0);//未结算
                        tOrderFee.setDetpId(ShiroKit.getUser().getDeptId());
                        tOrderFee.setDotId(ShiroKit.getUser().getDotId());
                        tOrderFee.setOrderId(order.getId());
                        tOrderFee.setFeeIncome(0);
                        orderFeeWarpper.insert(tOrderFee);
                    } else {
                        tOrderFee.setCreateUserId(ShiroKit.getUser().getId());
                        tOrderFee.setCreateTime(new Date());
                        tOrderFee.setMoney(Double.valueOf(sOrderFee.getS_tf()));
                        orderFeeWarpper.updateById(tOrderFee);
                    }
                } else if (tOrderFee.getFeeId() == 23 && tOrderFee.getMoney() != Double.valueOf(sOrderFee.getS_ds())) {
                    if (tOrderFee.getOrderId() == null) {
                        tOrderFee.setCreateUserId(ShiroKit.getUser().getId());
                        tOrderFee.setCreateTime(new Date());
                        tOrderFee.setMoney(Double.valueOf(sOrderFee.getS_ds()));
                        tOrderFee.setAuditStatus(0);//未结算
                        tOrderFee.setDetpId(ShiroKit.getUser().getDeptId());
                        tOrderFee.setDotId(ShiroKit.getUser().getDotId());
                        tOrderFee.setOrderId(order.getId());
                        tOrderFee.setFeeIncome(0);
                        orderFeeWarpper.insert(tOrderFee);
                    } else {
                        tOrderFee.setCreateUserId(ShiroKit.getUser().getId());
                        tOrderFee.setCreateTime(new Date());
                        tOrderFee.setMoney(Double.valueOf(sOrderFee.getS_ds()));
                        orderFeeWarpper.updateById(tOrderFee);
                    }
                } else if (tOrderFee.getFeeId() == 26 && tOrderFee.getMoney() != Double.valueOf(sOrderFee.getS_by())) {
                    if (tOrderFee.getOrderId() == null) {
                        tOrderFee.setCreateUserId(ShiroKit.getUser().getId());
                        tOrderFee.setCreateTime(new Date());
                        tOrderFee.setMoney(Double.valueOf(sOrderFee.getS_by()));
                        tOrderFee.setAuditStatus(0);//未结算
                        tOrderFee.setDetpId(ShiroKit.getUser().getDeptId());
                        tOrderFee.setDotId(ShiroKit.getUser().getDotId());
                        tOrderFee.setOrderId(order.getId());
                        tOrderFee.setFeeIncome(0);
                        orderFeeWarpper.insert(tOrderFee);
                    } else {
                        tOrderFee.setCreateUserId(ShiroKit.getUser().getId());
                        tOrderFee.setCreateTime(new Date());
                        tOrderFee.setMoney(Double.valueOf(sOrderFee.getS_by()));
                        orderFeeWarpper.updateById(tOrderFee);
                    }
                } else if (tOrderFee.getFeeId() == 25 && tOrderFee.getMoney() != Double.valueOf(sOrderFee.getS_sh())) {
                    if (tOrderFee.getOrderId() == null) {
                        tOrderFee.setCreateUserId(ShiroKit.getUser().getId());
                        tOrderFee.setCreateTime(new Date());
                        tOrderFee.setMoney(Double.valueOf(sOrderFee.getS_sh()));
                        tOrderFee.setAuditStatus(0);//未结算
                        tOrderFee.setDetpId(ShiroKit.getUser().getDeptId());
                        tOrderFee.setDotId(ShiroKit.getUser().getDotId());
                        tOrderFee.setOrderId(order.getId());
                        tOrderFee.setFeeIncome(0);
                        orderFeeWarpper.insert(tOrderFee);
                    } else {

                        tOrderFee.setCreateUserId(ShiroKit.getUser().getId());
                        tOrderFee.setCreateTime(new Date());
                        tOrderFee.setMoney(Double.valueOf(sOrderFee.getS_sh()));
                        orderFeeWarpper.updateById(tOrderFee);
                    }
                } else if (tOrderFee.getFeeId() == 24 && tOrderFee.getMoney() != Double.valueOf(sOrderFee.getS_zz())) {
                    if (tOrderFee.getOrderId() == null) {
                        tOrderFee.setCreateUserId(ShiroKit.getUser().getId());
                        tOrderFee.setCreateTime(new Date());
                        tOrderFee.setMoney(Double.valueOf(sOrderFee.getS_zz()));
                        tOrderFee.setAuditStatus(0);//未结算
                        tOrderFee.setDetpId(ShiroKit.getUser().getDeptId());
                        tOrderFee.setDotId(ShiroKit.getUser().getDotId());
                        tOrderFee.setOrderId(order.getId());
                        tOrderFee.setFeeIncome(0);
                        orderFeeWarpper.insert(tOrderFee);
                    } else {

                        tOrderFee.setCreateUserId(ShiroKit.getUser().getId());
                        tOrderFee.setCreateTime(new Date());
                        tOrderFee.setMoney(Double.valueOf(sOrderFee.getS_zz()));
                        orderFeeWarpper.updateById(tOrderFee);
                    }
                }
            }

        }

        List<TOrderFee> orderFees = orderFeeWarpper.selectOrderFee(order1.getId() + "", ids);
        // int stu=feeOrders.get(0).getAuditStatus();
        if (order1.getFlow_status() >= 3) {


//            Map param4=new HashMap();
//            param4.put("dot_id",ShiroKit.getUser().getDotId());
//            param4.put("nType", Const.NTYPE_04); // 类别 00 初始 01 配送 02 中转 03 自提  04 月结
//            String Balance_num = tDictMapper.getNumCode(param4);

            //0 开单 1 车次核销 2 出库 3 收银 4 核销
            TMonthbalanceDetail tMonthbalanceDetail = new TMonthbalanceDetail();
            tMonthbalanceDetail.setCreater(ShiroKit.getUser().getId());
            tMonthbalanceDetail.setCreatetime(new Date());
            tMonthbalanceDetail.setDotId(order1.getDotId());
            tMonthbalanceDetail.setIncome(5);
            tMonthbalanceDetail.setIncomeId(order1.getId());
            tMonthbalanceDetail.setIncomeName(order1.getOriginal_num());
            if (sOrderFee.getIsPayp().equals("0")) {
                tMonthbalanceDetail.setSettlementType(0);
                tMonthbalanceDetail.setStatus("1");
            } else {
                tMonthbalanceDetail.setSettlementType(1);
                tMonthbalanceDetail.setStatus("0");
            }


            tMonthbalanceDetail.setMoney(Double.valueOf(sOrderFee.getCashp()) + Double.valueOf(sOrderFee.getWeixinp())
                    + Double.valueOf(sOrderFee.getBankp()) + Double.valueOf(sOrderFee.getAlipayp()));
            String billNum = "TT" + ShiroKit.getUser().getId() + "_" + DateUtil.getAllTimes();
            TBill bill = new TBill();
            if (sOrderFee.getIsPayp().equals("0")) {
                bill.setDotId(order1.getDotId());
                bill.setCreateTime(new Date());
                bill.setBillNum(billNum);
                bill.setEditUser(ShiroKit.getUser().getId());
                bill.setCash(Float.valueOf(sOrderFee.getCashp()));
                bill.setBank(Float.valueOf(sOrderFee.getBankp()));
                bill.setAlipay(Float.valueOf(sOrderFee.getAlipayp()));
                bill.setWeixin(Float.valueOf(sOrderFee.getWeixinp()));
                if(!("0".equals(sOrderFee.getCashp())&&"0".equals(sOrderFee.getBankp())&&"0".equals(sOrderFee.getAlipayp())&&"0".equals(sOrderFee.getWeixinp()))){
                    billMapper.insert(bill);
                }
            }
            tMonthbalanceDetail.setBillId(bill.getId());
            TTrain tTrain = tTrainMapper.selectById(order1.getTrain_id());
            tMonthbalanceDetail.setTraner(tTrain.getDriverName());
            tMonthbalanceDetail.setBalance_num(tDictMapper.getNumCode(param3));
            if("0".equals(sOrderFee.getCashp())&&"0".equals(sOrderFee.getBankp())&&"0".equals(sOrderFee.getAlipayp())&&"0".equals(sOrderFee.getWeixinp())){
                tMonthbalanceDetail.setStatus(OrderEnum.MONTHBALANCE_STATUS_SETTLED.getCode()+"");
            }
            monthbalanceDetailMapper.insert(tMonthbalanceDetail);
            for (TOrderFee tOrderFee : orderFees) {
                tOrderFee.setId(null);
                if (tOrderFee.getFeeId() == 37 && tOrderFee.getMoney() != Double.valueOf(sOrderFee.getP_tf())) {
                    if (tOrderFee.getOrderId() == null) {
                        if (sOrderFee.getIsPayp().equals("0")) {//现结
                            tOrderFee.setSettlementType("0");
                        } else {
                            tOrderFee.setSettlementType("1");

                        }
                        if (order1.getFlow_status() == 4) {
                            tOrderFee.setAuditStatus(3);
                        } else {
                            tOrderFee.setAuditStatus(1);
                        }
                        tOrderFee.setDetpId(ShiroKit.getUser().getDeptId());
                        tOrderFee.setDotId(ShiroKit.getUser().getDotId());
                        tOrderFee.setOrderId(order.getId());
                        tOrderFee.setFeeIncome(0);
                    }
                    tOrderFee.setCreateUserId(ShiroKit.getUser().getId());
                    tOrderFee.setCreateTime(new Date());
                    tOrderFee.setMoney(Double.valueOf(sOrderFee.getP_tf()) - tOrderFee.getMoney());
                    tOrderFee.setBalance_id(tMonthbalanceDetail.getId());
                    orderFeeWarpper.insert(tOrderFee);

                } else if (tOrderFee.getFeeId() == 42 && tOrderFee.getMoney() != Double.valueOf(sOrderFee.getP_ld())) {
                    if (tOrderFee.getOrderId() == null) {
                        if (sOrderFee.getIsPayp().equals("0")) {//现结
                            tOrderFee.setSettlementType("0");
                        } else {
                            tOrderFee.setSettlementType("1");

                        }
                        if (order1.getFlow_status() == 4) {
                            tOrderFee.setAuditStatus(3);
                        } else {
                            tOrderFee.setAuditStatus(1);
                        }
                        tOrderFee.setDetpId(ShiroKit.getUser().getDeptId());
                        tOrderFee.setDotId(ShiroKit.getUser().getDotId());
                        tOrderFee.setOrderId(order.getId());
                        tOrderFee.setFeeIncome(0);
                    }
                    tOrderFee.setCreateUserId(ShiroKit.getUser().getId());
                    tOrderFee.setCreateTime(new Date());
                    tOrderFee.setMoney(Double.valueOf(sOrderFee.getP_ld()) - tOrderFee.getMoney());
                    tOrderFee.setBalance_id(tMonthbalanceDetail.getId());
                    orderFeeWarpper.insert(tOrderFee);

                } else if (tOrderFee.getFeeId() == 38 && tOrderFee.getMoney() != Double.valueOf(sOrderFee.getP_ds())) {
                    if (tOrderFee.getOrderId() == null) {
                        if (sOrderFee.getIsPayp().equals("0")) {//现结
                            tOrderFee.setSettlementType("0");
                        } else {
                            tOrderFee.setSettlementType("1");

                        }
                        if (order1.getFlow_status() == 4) {
                            tOrderFee.setAuditStatus(3);
                        } else {
                            tOrderFee.setAuditStatus(1);
                        }
                        tOrderFee.setDetpId(ShiroKit.getUser().getDeptId());
                        tOrderFee.setDotId(ShiroKit.getUser().getDotId());
                        tOrderFee.setOrderId(order.getId());
                        tOrderFee.setFeeIncome(0);
                    }
                    tOrderFee.setCreateUserId(ShiroKit.getUser().getId());
                    tOrderFee.setBalance_id(tMonthbalanceDetail.getId());
                    tOrderFee.setCreateTime(new Date());
                    tOrderFee.setMoney(Double.valueOf(sOrderFee.getP_ds()) - tOrderFee.getMoney());
                    orderFeeWarpper.insert(tOrderFee);
                } else if (tOrderFee.getFeeId() == 28 && tOrderFee.getMoney() != Double.valueOf(sOrderFee.getP_by())) {
                    if (tOrderFee.getOrderId() == null) {
                        if (sOrderFee.getIsPayp().equals("0")) {//现结
                            tOrderFee.setSettlementType("0");
                        } else {
                            tOrderFee.setSettlementType("1");

                        }
                        if (order1.getFlow_status() == 4) {
                            tOrderFee.setAuditStatus(3);
                        } else {
                            tOrderFee.setAuditStatus(1);
                        }
                        tOrderFee.setDetpId(ShiroKit.getUser().getDeptId());
                        tOrderFee.setDotId(ShiroKit.getUser().getDotId());
                        tOrderFee.setOrderId(order.getId());
                        tOrderFee.setFeeIncome(0);
                    }
                    tOrderFee.setCreateUserId(ShiroKit.getUser().getId());
                    tOrderFee.setBalance_id(tMonthbalanceDetail.getId());
                    tOrderFee.setCreateTime(new Date());
                    tOrderFee.setMoney(Double.valueOf(sOrderFee.getP_by()) - tOrderFee.getMoney());
                    orderFeeWarpper.insert(tOrderFee);


                } else if (tOrderFee.getFeeId() == 29 && tOrderFee.getMoney() != Double.valueOf(sOrderFee.getP_cc())) {
                    if (tOrderFee.getOrderId() == null) {
                        if (sOrderFee.getIsPayp().equals("0")) {//现结
                            tOrderFee.setSettlementType("0");
                        } else {
                            tOrderFee.setSettlementType("1");

                        }
                        if (order1.getFlow_status() == 4) {
                            tOrderFee.setAuditStatus(3);
                        } else {
                            tOrderFee.setAuditStatus(1);
                        }
                        tOrderFee.setDetpId(ShiroKit.getUser().getDeptId());
                        tOrderFee.setDotId(ShiroKit.getUser().getDotId());
                        tOrderFee.setOrderId(order.getId());
                        tOrderFee.setFeeIncome(0);
                    }
                    tOrderFee.setCreateUserId(ShiroKit.getUser().getId());
                    tOrderFee.setBalance_id(tMonthbalanceDetail.getId());
                    tOrderFee.setCreateTime(new Date());
                    tOrderFee.setMoney(Double.valueOf(sOrderFee.getP_cc()) - tOrderFee.getMoney());
                    orderFeeWarpper.insert(tOrderFee);

                } else if (tOrderFee.getFeeId() == 27 && tOrderFee.getMoney() != Double.valueOf(sOrderFee.getP_sh())) {
                    if (tOrderFee.getOrderId() == null) {
                        if (sOrderFee.getIsPayp().equals("0")) {//现结
                            tOrderFee.setSettlementType("0");
                        } else {
                            tOrderFee.setSettlementType("1");

                        }
                        if (order1.getFlow_status() == 4) {
                            tOrderFee.setAuditStatus(3);
                        } else {
                            tOrderFee.setAuditStatus(1);
                        }
                        tOrderFee.setDetpId(ShiroKit.getUser().getDeptId());
                        tOrderFee.setDotId(ShiroKit.getUser().getDotId());
                        tOrderFee.setOrderId(order.getId());
                        tOrderFee.setFeeIncome(0);
                    }
                    tOrderFee.setCreateUserId(ShiroKit.getUser().getId());
                    tOrderFee.setBalance_id(tMonthbalanceDetail.getId());
                    tOrderFee.setCreateTime(new Date());
                    tOrderFee.setMoney(Double.valueOf(sOrderFee.getP_sh()) - tOrderFee.getMoney());
                    orderFeeWarpper.insert(tOrderFee);

                } else if (tOrderFee.getFeeId() == 30 && tOrderFee.getMoney() != Double.valueOf(sOrderFee.getP_cf())) {
                    if (tOrderFee.getOrderId() == null) {
                        if (sOrderFee.getIsPayp().equals("0")) {//现结
                            tOrderFee.setSettlementType("0");
                        } else {
                            tOrderFee.setSettlementType("1");

                        }
                        if (order1.getFlow_status() == 4) {
                            tOrderFee.setAuditStatus(3);
                        } else {
                            tOrderFee.setAuditStatus(1);
                        }
                        tOrderFee.setDetpId(ShiroKit.getUser().getDeptId());
                        tOrderFee.setDotId(ShiroKit.getUser().getDotId());
                        tOrderFee.setOrderId(order.getId());
                        tOrderFee.setFeeIncome(0);
                    }
                    tOrderFee.setCreateUserId(ShiroKit.getUser().getId());
                    tOrderFee.setBalance_id(tMonthbalanceDetail.getId());
                    tOrderFee.setCreateTime(new Date());
                    tOrderFee.setMoney(Double.valueOf(sOrderFee.getP_cf()) - tOrderFee.getMoney());
                    orderFeeWarpper.insert(tOrderFee);

                }
//                if (sOrderFee.getIsPayp().equals("0") && tOrderFee.getId() != null) {
//                    Map param = new HashMap();
//                    param.put("order_fee_id", tOrderFee.getId() + "");
//                    param.put("order_id", tOrderFee.getOrderId() + "");
//                    param.put("bill_id", bill.getId());
//                    tTrainMapper.addFeeBill(param);
//                }
            }
        } else {
            //更新数据
            for (TOrderFee tOrderFee : orderFees) {
                if (tOrderFee.getFeeId() == 37 && tOrderFee.getMoney() != Double.valueOf(sOrderFee.getP_tf())) {
                    if (tOrderFee.getOrderId() == null) {
                        tOrderFee.setCreateUserId(ShiroKit.getUser().getId());
                        tOrderFee.setCreateTime(new Date());
                        tOrderFee.setMoney(Double.valueOf(sOrderFee.getP_tf()));
                        tOrderFee.setAuditStatus(0);//未结算
                        tOrderFee.setDetpId(ShiroKit.getUser().getDeptId());
                        tOrderFee.setDotId(ShiroKit.getUser().getDotId());
                        tOrderFee.setOrderId(order.getId());
                        tOrderFee.setFeeIncome(0);
                        orderFeeWarpper.insert(tOrderFee);
                    } else {
                        tOrderFee.setCreateUserId(ShiroKit.getUser().getId());
                        tOrderFee.setCreateTime(new Date());
                        tOrderFee.setMoney(Double.valueOf(sOrderFee.getP_tf()));
                        orderFeeWarpper.updateById(tOrderFee);
                    }

                } else if (tOrderFee.getFeeId() == 42 && tOrderFee.getMoney() != Double.valueOf(sOrderFee.getP_ld())) {
                    if (tOrderFee.getOrderId() == null) {
                        tOrderFee.setCreateUserId(ShiroKit.getUser().getId());
                        tOrderFee.setCreateTime(new Date());
                        tOrderFee.setMoney(Double.valueOf(sOrderFee.getP_ld()));
                        tOrderFee.setAuditStatus(0);//未结算
                        tOrderFee.setDetpId(ShiroKit.getUser().getDeptId());
                        tOrderFee.setDotId(ShiroKit.getUser().getDotId());
                        tOrderFee.setOrderId(order.getId());
                        tOrderFee.setFeeIncome(0);
                        orderFeeWarpper.insert(tOrderFee);
                    } else {
                        tOrderFee.setCreateUserId(ShiroKit.getUser().getId());
                        tOrderFee.setCreateTime(new Date());
                        tOrderFee.setMoney(Double.valueOf(sOrderFee.getP_ld()));
                        orderFeeWarpper.updateById(tOrderFee);
                    }

                } else if (tOrderFee.getFeeId() == 38 && tOrderFee.getMoney() != Double.valueOf(sOrderFee.getP_ds())) {
                    if (tOrderFee.getOrderId() == null) {
                        tOrderFee.setCreateUserId(ShiroKit.getUser().getId());
                        tOrderFee.setCreateTime(new Date());
                        tOrderFee.setMoney(Double.valueOf(sOrderFee.getP_ds()));
                        tOrderFee.setAuditStatus(0);//未结算
                        tOrderFee.setDetpId(ShiroKit.getUser().getDeptId());
                        tOrderFee.setDotId(ShiroKit.getUser().getDotId());
                        tOrderFee.setOrderId(order.getId());
                        tOrderFee.setFeeIncome(0);
                        orderFeeWarpper.insert(tOrderFee);
                    } else {
                        tOrderFee.setCreateUserId(ShiroKit.getUser().getId());
                        tOrderFee.setCreateTime(new Date());
                        tOrderFee.setMoney(Double.valueOf(sOrderFee.getP_ds()));
                        orderFeeWarpper.updateById(tOrderFee);
                    }

                } else if (tOrderFee.getFeeId() == 27 && tOrderFee.getMoney() != Double.valueOf(sOrderFee.getP_sh())) {
                    if (tOrderFee.getOrderId() == null) {
                        tOrderFee.setCreateUserId(ShiroKit.getUser().getId());
                        tOrderFee.setCreateTime(new Date());
                        tOrderFee.setMoney(Double.valueOf(sOrderFee.getP_sh()));
                        tOrderFee.setAuditStatus(0);//未结算
                        tOrderFee.setDetpId(ShiroKit.getUser().getDeptId());
                        tOrderFee.setDotId(ShiroKit.getUser().getDotId());
                        tOrderFee.setOrderId(order.getId());
                        tOrderFee.setFeeIncome(0);
                        orderFeeWarpper.insert(tOrderFee);
                    } else {
                        tOrderFee.setCreateUserId(ShiroKit.getUser().getId());
                        tOrderFee.setCreateTime(new Date());
                        tOrderFee.setMoney(Double.valueOf(sOrderFee.getP_sh()));
                        orderFeeWarpper.updateById(tOrderFee);
                    }
                } else if (tOrderFee.getFeeId() == 28 && tOrderFee.getMoney() != Double.valueOf(sOrderFee.getP_by())) {
                    if (tOrderFee.getOrderId() == null) {
                        tOrderFee.setCreateUserId(ShiroKit.getUser().getId());
                        tOrderFee.setCreateTime(new Date());
                        tOrderFee.setMoney(Double.valueOf(sOrderFee.getP_by()));
                        tOrderFee.setAuditStatus(0);//未结算
                        tOrderFee.setDetpId(ShiroKit.getUser().getDeptId());
                        tOrderFee.setDotId(ShiroKit.getUser().getDotId());
                        tOrderFee.setOrderId(order.getId());
                        tOrderFee.setFeeIncome(0);
                        orderFeeWarpper.insert(tOrderFee);
                    } else {
                        tOrderFee.setCreateUserId(ShiroKit.getUser().getId());
                        tOrderFee.setCreateTime(new Date());
                        tOrderFee.setMoney(Double.valueOf(sOrderFee.getP_by()));
                        orderFeeWarpper.updateById(tOrderFee);
                    }
                } else if (tOrderFee.getFeeId() == 29 && tOrderFee.getMoney() != Double.valueOf(sOrderFee.getP_cc())) {
                    if (tOrderFee.getOrderId() == null) {
                        tOrderFee.setCreateUserId(ShiroKit.getUser().getId());
                        tOrderFee.setCreateTime(new Date());
                        tOrderFee.setMoney(Double.valueOf(sOrderFee.getP_cc()));
                        tOrderFee.setAuditStatus(0);//未结算
                        tOrderFee.setDetpId(ShiroKit.getUser().getDeptId());
                        tOrderFee.setDotId(ShiroKit.getUser().getDotId());
                        tOrderFee.setOrderId(order.getId());
                        tOrderFee.setFeeIncome(0);
                        orderFeeWarpper.insert(tOrderFee);
                    } else {
                        tOrderFee.setCreateUserId(ShiroKit.getUser().getId());
                        tOrderFee.setCreateTime(new Date());
                        tOrderFee.setMoney(Double.valueOf(sOrderFee.getP_cc()));
                        orderFeeWarpper.updateById(tOrderFee);
                    }
                } else if (tOrderFee.getFeeId() == 30 && tOrderFee.getMoney() != Double.valueOf(sOrderFee.getP_cf())) {
                    if (tOrderFee.getOrderId() == null) {
                        tOrderFee.setCreateUserId(ShiroKit.getUser().getId());
                        tOrderFee.setCreateTime(new Date());
                        tOrderFee.setMoney(Double.valueOf(sOrderFee.getP_cf()));
                        tOrderFee.setAuditStatus(0);//未结算
                        tOrderFee.setDetpId(ShiroKit.getUser().getDeptId());
                        tOrderFee.setDotId(ShiroKit.getUser().getDotId());
                        tOrderFee.setOrderId(order.getId());
                        tOrderFee.setFeeIncome(0);
                        orderFeeWarpper.insert(tOrderFee);
                    } else {
                        tOrderFee.setCreateUserId(ShiroKit.getUser().getId());
                        tOrderFee.setCreateTime(new Date());
                        tOrderFee.setMoney(Double.valueOf(sOrderFee.getP_cf()));
                        orderFeeWarpper.updateById(tOrderFee);
                    }
                }
            }

        }

        if(receive_id!=null){
            OrderBackRecord orderReceive=new OrderBackRecord();
            orderReceive.setId(receive_id);
            if(receive_time!=null&&!receive_time.isEmpty()){
                orderReceive.setReceiveTime(DateUtil.parseDate(receive_time));
            }
            orderReceive.setRemark(remark);
            orderReceive.setReceiver(receiver);
            orderReceive.setEdit_time(new Date());
            orderReceive.setEdit_user(ShiroKit.getUser().getId());
            backRecordMapper.updateById(orderReceive);
        }
        if(send_id!=null){
            OrderBackRecord orderSend=new OrderBackRecord();
            orderSend.setId(send_id);
            if(send_Time!=null&&!send_Time.isEmpty()){
                orderSend.setSendTime(DateUtil.parseDate(send_Time));
            }
            orderSend.setRemark(remark);
            orderSend.setReceiver(sender);
            orderSend.setProcessMethod(processMethod);
            orderSend.setEdit_time(new Date());
            orderSend.setEdit_user(ShiroKit.getUser().getId());
            backRecordMapper.updateById(orderSend);
        }
//        orderBackRecord.setId(null);
//        if (!StringUtils.isEmpty(getPara("send_Time"))) {
//            orderBackRecord.setSendTime(DateUtil.parseDate(getPara("send_Time")));
//        }
//        orderBackRecord.setEdit_time(new Date());
//        orderBackRecord.setType(1);
//        orderBackRecord.setEdit_user(ShiroKit.getUser().getId());
//        if (!StringUtils.isEmpty(getPara("bacrRecord"))) {
//            int bacrRecordid = Integer.valueOf(getPara("bacrRecord"));
//            orderBackRecord.setId(bacrRecordid);
//        }
//
//        if (orderBackRecord.getId() == null) {
//            backRecordMapper.insert(orderBackRecord);
//            order.setOrder_back_receive_id(orderBackRecord.getId());
//        } else {
//            backRecordMapper.updateById(orderBackRecord);
//            order.setOrder_back_receive_id(orderBackRecord.getId());
//        }
//        if (orderBackRecord.getProcessMethod() != null && orderBackRecord.getProcessMethod() != 0) {
//            order.setReceiptStatus(2);
//        }

        order.setEdit_user(ShiroKit.getUser().getId());
        order.setEdit_time(new Date());
        orderMapper.updateById(order);

        return super.SUCCESS_TIP;
    }


    //修改中转车费
    @RequestMapping(value = "/refOrderAndOrderFeeZZ")
    @ResponseBody
    @Transactional
    public Object refOrderAndOrderFeeZZ(TOrder order, String  send_Time,String  receive_time,String sender,String receiver,Integer send_id,Integer processMethod,Integer receive_id,String remark, SOrderFee sOrderFee) {


        TOrder order1 = orderMapper.selectById(order.getId());
        if (sOrderFee.getModeFunc().equals("0")) {
            order.setMode(0);
        } else if (sOrderFee.getModeFunc().equals("1")) {
            order.setMode(1);
        }
        List<Integer> ids = new ArrayList<>();
        ids.add(32); ids.add(33);ids.add(34);ids.add(35);ids.add(36);ids.add(43);
        //上家的费用
        List<Integer> mIds = new ArrayList<>();
        mIds.add(22);mIds.add(23);mIds.add(24);mIds.add(25);mIds.add(26);
        List<TOrderFee> mOrderFees = orderFeeWarpper.selectOrderFee(order1.getId() + "", mIds);

        Map param3=new HashMap();
        param3.put("dot_id",ShiroKit.getUser().getDotId());
        param3.put("nType", Const.NTYPE_04); // 类别 00 初始 01 配送 02 中转 03 自提  04 月结
//        String bath_no = tDictMapper.getNumCode(param3);

        TMonthbalanceDetail sMonthbalanceDetail = new TMonthbalanceDetail();
        sMonthbalanceDetail.setCreater(ShiroKit.getUser().getId());
        sMonthbalanceDetail.setCreatetime(new Date());
        sMonthbalanceDetail.setDotId(order1.getDotId());
        sMonthbalanceDetail.setIncome(5);
        sMonthbalanceDetail.setIncomeId(order1.getId());
        sMonthbalanceDetail.setIncomeName(order1.getOriginal_num());
        if (sOrderFee.getIsPay().equals("0")) {//现结
            sMonthbalanceDetail.setSettlementType(0);
            sMonthbalanceDetail.setStatus("1");
        } else {
            sMonthbalanceDetail.setSettlementType(1);
            sMonthbalanceDetail.setStatus("0");
        }
        sMonthbalanceDetail.setMoney(Double.valueOf(sOrderFee.getA1()));
        TBill sBill = new TBill();
        if (sOrderFee.getIsPay().equals("0")) {
            String billNum = "TT" + ShiroKit.getUser().getId() + "_" + DateUtil.getAllTimes();
            sBill.setDotId(order1.getDotId());
            sBill.setCreateTime(new Date());
            sBill.setBillNum(billNum);
            sBill.setCash(Float.valueOf(sOrderFee.getCashs()));
            sBill.setBank(Float.valueOf(sOrderFee.getBanks()));
            sBill.setAlipay(Float.valueOf(sOrderFee.getAlipays()));
            sBill.setWeixin(Float.valueOf(sOrderFee.getWeixins()));
            sBill.setEditUser(ShiroKit.getUser().getId());
            if(!("0".equals(sOrderFee.getCashs())&&"0".equals(sOrderFee.getBanks())&&"0".equals(sOrderFee.getAlipays())&&"0".equals(sOrderFee.getWeixins()))){
                billMapper.insert(sBill);
            }
        }
        sMonthbalanceDetail.setBillId(sBill.getId());
        sMonthbalanceDetail.setTraner(order1.getSendDeptName());
        if("0".equals(sOrderFee.getA1())){
            sMonthbalanceDetail.setStatus(OrderEnum.MONTHBALANCE_STATUS_SETTLED.getCode()+"");
        }
            sMonthbalanceDetail.setBalance_num(tDictMapper.getNumCode(param3));

        monthbalanceDetailMapper.insert(sMonthbalanceDetail);
        if (order1.getFlow_status() > 0) {
            for (TOrderFee tOrderFee : mOrderFees) {//上家的费用都是新增
                tOrderFee.setId(null);
                if (tOrderFee.getFeeId() == 22 && tOrderFee.getMoney() != Double.valueOf(sOrderFee.getS_tf())) {
                    tOrderFee.setCreateUserId(ShiroKit.getUser().getId());
                    tOrderFee.setCreateTime(new Date());

                    tOrderFee.setBalance_id(sMonthbalanceDetail.getId());
                    if (Double.valueOf(sOrderFee.getS_tf()) - tOrderFee.getMoney() != 0) {
                        tOrderFee.setMoney(Double.valueOf(sOrderFee.getS_tf()) - tOrderFee.getMoney());
                        if (tOrderFee.getOrderId() == null) {
                            if (sOrderFee.getIsPay().equals("0")) {
                                tOrderFee.setSettlementType("0");
                            } else {
                                tOrderFee.setSettlementType("1");

                            }
                            if (order1.getFlow_status() == 4) {
                                tOrderFee.setAuditStatus(3);
                            } else {
                                tOrderFee.setAuditStatus(1);
                            }
                            tOrderFee.setDetpId(ShiroKit.getUser().getDeptId());
                            tOrderFee.setDotId(ShiroKit.getUser().getDotId());
                            tOrderFee.setOrderId(order.getId());
                            tOrderFee.setFeeIncome(0);
                        }
                        orderFeeWarpper.insert(tOrderFee);
                    }

                } else if (tOrderFee.getFeeId() == 23 && tOrderFee.getMoney() != Double.valueOf(sOrderFee.getS_ds())) {
                    tOrderFee.setCreateUserId(ShiroKit.getUser().getId());
                    tOrderFee.setBalance_id(sMonthbalanceDetail.getId());
                    tOrderFee.setCreateTime(new Date());
                    if (Double.valueOf(sOrderFee.getS_ds()) - tOrderFee.getMoney() != 0) {
                        tOrderFee.setMoney(Double.valueOf(sOrderFee.getS_ds()) - tOrderFee.getMoney());
                        if (tOrderFee.getOrderId() == null) {
                            if (sOrderFee.getIsPay().equals("0")) {
                                tOrderFee.setSettlementType("0");
                            } else {
                                tOrderFee.setSettlementType("1");

                            }
                            if (order1.getFlow_status() == 4) {
                                tOrderFee.setAuditStatus(3);
                            } else {
                                tOrderFee.setAuditStatus(1);
                            }
                            tOrderFee.setDetpId(ShiroKit.getUser().getDeptId());
                            tOrderFee.setDotId(ShiroKit.getUser().getDotId());
                            tOrderFee.setOrderId(order.getId());
                            tOrderFee.setFeeIncome(0);
                        }
                        orderFeeWarpper.insert(tOrderFee);
                    }


                } else if (tOrderFee.getFeeId() == 26 && tOrderFee.getMoney() != Double.valueOf(sOrderFee.getS_by())) {
                    tOrderFee.setCreateUserId(ShiroKit.getUser().getId());
                    tOrderFee.setBalance_id(sMonthbalanceDetail.getId());
                    tOrderFee.setCreateTime(new Date());
                    if (Double.valueOf(sOrderFee.getS_by()) - tOrderFee.getMoney() != 0) {
                        tOrderFee.setMoney(Double.valueOf(sOrderFee.getS_by()) - tOrderFee.getMoney());
                        if (tOrderFee.getOrderId() == null) {
                            if (sOrderFee.getIsPay().equals("0")) {
                                tOrderFee.setSettlementType("0");
                            } else {
                                tOrderFee.setSettlementType("1");

                            }
                            if (order1.getFlow_status() == 4) {
                                tOrderFee.setAuditStatus(3);
                            } else {
                                tOrderFee.setAuditStatus(1);
                            }
                            tOrderFee.setDetpId(ShiroKit.getUser().getDeptId());
                            tOrderFee.setDotId(ShiroKit.getUser().getDotId());
                            tOrderFee.setOrderId(order.getId());
                            tOrderFee.setFeeIncome(0);
                        }
                        orderFeeWarpper.insert(tOrderFee);
                    }

                } else if (tOrderFee.getFeeId() == 25 && tOrderFee.getMoney() != Double.valueOf(sOrderFee.getS_sh())) {
                    tOrderFee.setCreateUserId(ShiroKit.getUser().getId());
                    tOrderFee.setBalance_id(sMonthbalanceDetail.getId());
                    tOrderFee.setCreateTime(new Date());
                    if (Double.valueOf(sOrderFee.getS_sh()) - tOrderFee.getMoney() != 0) {
                        tOrderFee.setMoney(Double.valueOf(sOrderFee.getS_sh()) - tOrderFee.getMoney());
                        if (tOrderFee.getOrderId() == null) {
                            if (sOrderFee.getIsPay().equals("0")) {
                                tOrderFee.setSettlementType("0");
                            } else {
                                tOrderFee.setSettlementType("1");

                            }
                            if (order1.getFlow_status() == 4) {
                                tOrderFee.setAuditStatus(3);
                            } else {
                                tOrderFee.setAuditStatus(1);
                            }
                            tOrderFee.setDetpId(ShiroKit.getUser().getDeptId());
                            tOrderFee.setDotId(ShiroKit.getUser().getDotId());
                            tOrderFee.setOrderId(order.getId());
                            tOrderFee.setFeeIncome(0);
                        }
                        orderFeeWarpper.insert(tOrderFee);
                    }

                } else if (tOrderFee.getFeeId() == 24 && tOrderFee.getMoney() != Double.valueOf(sOrderFee.getS_zz())) {
                    tOrderFee.setCreateUserId(ShiroKit.getUser().getId());
                    tOrderFee.setBalance_id(sMonthbalanceDetail.getId());
                    tOrderFee.setCreateTime(new Date());
                    if (Double.valueOf(sOrderFee.getS_zz()) - tOrderFee.getMoney() != 0) {
                        tOrderFee.setMoney(Double.valueOf(sOrderFee.getS_zz()) - tOrderFee.getMoney());
                        if (tOrderFee.getOrderId() == null) {
                            if (sOrderFee.getIsPay().equals("0")) {
                                tOrderFee.setSettlementType("0");
                            } else {
                                tOrderFee.setSettlementType("1");

                            }
                            if (order1.getFlow_status() == 4) {
                                tOrderFee.setAuditStatus(3);
                            } else {
                                tOrderFee.setAuditStatus(1);
                            }
                            tOrderFee.setDetpId(ShiroKit.getUser().getDeptId());
                            tOrderFee.setDotId(ShiroKit.getUser().getDotId());
                            tOrderFee.setOrderId(order.getId());
                            tOrderFee.setFeeIncome(0);
                        }
                        orderFeeWarpper.insert(tOrderFee);
                    }
                }

            }
        } else {

            for (TOrderFee tOrderFee : mOrderFees) {
                if (tOrderFee.getFeeId() == 22 && tOrderFee.getMoney() != Double.valueOf(sOrderFee.getS_tf())) {
                    if (tOrderFee.getOrderId() == null) {
                        tOrderFee.setCreateUserId(ShiroKit.getUser().getId());
                        tOrderFee.setCreateTime(new Date());
                        tOrderFee.setMoney(Double.valueOf(sOrderFee.getS_tf()));
                        tOrderFee.setAuditStatus(0);//未结算
                        tOrderFee.setDetpId(ShiroKit.getUser().getDeptId());
                        tOrderFee.setDotId(ShiroKit.getUser().getDotId());
                        tOrderFee.setOrderId(order.getId());
                        tOrderFee.setFeeIncome(0);
                        orderFeeWarpper.insert(tOrderFee);
                    } else {
                        tOrderFee.setCreateUserId(ShiroKit.getUser().getId());
                        tOrderFee.setCreateTime(new Date());
                        tOrderFee.setMoney(Double.valueOf(sOrderFee.getS_tf()));
                        orderFeeWarpper.updateById(tOrderFee);
                    }
                } else if (tOrderFee.getFeeId() == 23 && tOrderFee.getMoney() != Double.valueOf(sOrderFee.getS_ds())) {
                    if (tOrderFee.getOrderId() == null) {
                        tOrderFee.setCreateUserId(ShiroKit.getUser().getId());
                        tOrderFee.setCreateTime(new Date());
                        tOrderFee.setMoney(Double.valueOf(sOrderFee.getS_ds()));
                        tOrderFee.setAuditStatus(0);//未结算
                        tOrderFee.setDetpId(ShiroKit.getUser().getDeptId());
                        tOrderFee.setDotId(ShiroKit.getUser().getDotId());
                        tOrderFee.setOrderId(order.getId());
                        tOrderFee.setFeeIncome(0);
                        orderFeeWarpper.insert(tOrderFee);
                    } else {
                        tOrderFee.setCreateUserId(ShiroKit.getUser().getId());
                        tOrderFee.setCreateTime(new Date());
                        tOrderFee.setMoney(Double.valueOf(sOrderFee.getS_ds()));
                        orderFeeWarpper.updateById(tOrderFee);
                    }
                } else if (tOrderFee.getFeeId() == 26 && tOrderFee.getMoney() != Double.valueOf(sOrderFee.getS_by())) {
                    if (tOrderFee.getOrderId() == null) {
                        tOrderFee.setCreateUserId(ShiroKit.getUser().getId());
                        tOrderFee.setCreateTime(new Date());
                        tOrderFee.setMoney(Double.valueOf(sOrderFee.getS_by()));
                        tOrderFee.setAuditStatus(0);//未结算
                        tOrderFee.setDetpId(ShiroKit.getUser().getDeptId());
                        tOrderFee.setDotId(ShiroKit.getUser().getDotId());
                        tOrderFee.setOrderId(order.getId());
                        tOrderFee.setFeeIncome(0);
                        orderFeeWarpper.insert(tOrderFee);
                    } else {
                        tOrderFee.setCreateUserId(ShiroKit.getUser().getId());
                        tOrderFee.setCreateTime(new Date());
                        tOrderFee.setMoney(Double.valueOf(sOrderFee.getS_by()));
                        orderFeeWarpper.updateById(tOrderFee);
                    }
                } else if (tOrderFee.getFeeId() == 25 && tOrderFee.getMoney() != Double.valueOf(sOrderFee.getS_sh())) {
                    if (tOrderFee.getOrderId() == null) {
                        tOrderFee.setCreateUserId(ShiroKit.getUser().getId());
                        tOrderFee.setCreateTime(new Date());
                        tOrderFee.setMoney(Double.valueOf(sOrderFee.getS_sh()));
                        tOrderFee.setAuditStatus(0);//未结算
                        tOrderFee.setDetpId(ShiroKit.getUser().getDeptId());
                        tOrderFee.setDotId(ShiroKit.getUser().getDotId());
                        tOrderFee.setOrderId(order.getId());
                        tOrderFee.setFeeIncome(0);
                        orderFeeWarpper.insert(tOrderFee);
                    } else {
                        tOrderFee.setCreateUserId(ShiroKit.getUser().getId());
                        tOrderFee.setCreateTime(new Date());
                        tOrderFee.setMoney(Double.valueOf(sOrderFee.getS_sh()));
                        orderFeeWarpper.updateById(tOrderFee);
                    }
                } else if (tOrderFee.getFeeId() == 24 && tOrderFee.getMoney() != Double.valueOf(sOrderFee.getS_zz())) {
                    if (tOrderFee.getOrderId() == null) {
                        tOrderFee.setCreateUserId(ShiroKit.getUser().getId());
                        tOrderFee.setCreateTime(new Date());
                        tOrderFee.setMoney(Double.valueOf(sOrderFee.getS_zz()));
                        tOrderFee.setAuditStatus(0);//未结算
                        tOrderFee.setDetpId(ShiroKit.getUser().getDeptId());
                        tOrderFee.setDotId(ShiroKit.getUser().getDotId());
                        tOrderFee.setOrderId(order.getId());
                        tOrderFee.setFeeIncome(0);
                        orderFeeWarpper.insert(tOrderFee);
                    } else {
                        tOrderFee.setCreateUserId(ShiroKit.getUser().getId());
                        tOrderFee.setCreateTime(new Date());
                        tOrderFee.setMoney(Double.valueOf(sOrderFee.getS_zz()));
                        orderFeeWarpper.updateById(tOrderFee);
                    }
                }
            }

        }
        List<TOrderFee> orderFees = orderFeeWarpper.selectOrderFee(order1.getId() + "", ids);
        //int stu=feeOrders.get(0).getAuditStatus();
        if (order1.getFlow_status() >= 3) {
            //已收银后，就要新增order_fee记录，未收银就更新order_fee记录
            String billNum = "TT" + ShiroKit.getUser().getId() + "_" + DateUtil.getAllTimes();
            TBill bill = new TBill();
            if (sOrderFee.getIsPayzz().equals("0")) {
                bill.setDotId(order1.getDotId());
                bill.setCreateTime(new Date());
                bill.setBillNum(billNum);
                bill.setEditUser(ShiroKit.getUser().getId());
                bill.setCash(Float.valueOf(sOrderFee.getCashzz()));
                bill.setBank(Float.valueOf(sOrderFee.getBankzz()));
                bill.setAlipay(Float.valueOf(sOrderFee.getAlipayzz()));
                bill.setWeixin(Float.valueOf(sOrderFee.getWeixinzz()));
                if(!("0".equals(sOrderFee.getCashzz())&&"0".equals(sOrderFee.getBankzz())&&"0".equals(sOrderFee.getAlipayzz())&&"0".equals(sOrderFee.getWeixinzz()))){
                    billMapper.insert(bill);
                }
            }
            TMonthbalanceDetail tMonthbalanceDetail = new TMonthbalanceDetail();
            if (4 == order1.getFlow_status() || "0".equals(sOrderFee.getIsPayzz())) {

                //核销 或者现金收银才需要插入,否者核销的时候生成重复数据
                tMonthbalanceDetail.setCreater(ShiroKit.getUser().getId());
                tMonthbalanceDetail.setCreatetime(new Date());
                tMonthbalanceDetail.setDotId(order1.getDotId());
                tMonthbalanceDetail.setIncome(5);
                tMonthbalanceDetail.setIncomeId(order1.getId());
                tMonthbalanceDetail.setIncomeName(order1.getOriginal_num());
                if (sOrderFee.getIsPayzz().equals("0")) {
                    tMonthbalanceDetail.setSettlementType(0);
                    tMonthbalanceDetail.setStatus("1");
                } else {
                    tMonthbalanceDetail.setSettlementType(1);
                    tMonthbalanceDetail.setStatus("0");
                }
                if("0".equals(sOrderFee.getA4())){
                    tMonthbalanceDetail.setStatus(OrderEnum.MONTHBALANCE_STATUS_SETTLED.getCode()+"");
                }
                tMonthbalanceDetail.setMoney(Double.valueOf(sOrderFee.getA4()));
                tMonthbalanceDetail.setTraner(order.getTransfer_name());
                tMonthbalanceDetail.setBalance_num(tDictMapper.getNumCode(param3));
                tMonthbalanceDetail.setBillId(bill.getId());
                monthbalanceDetailMapper.insert(tMonthbalanceDetail);
            }

            for (TOrderFee tOrderFee : orderFees) {
                tOrderFee.setId(null);
                if (order1.getFlow_status() >= 3) {
                    tOrderFee.setSettlementType(sOrderFee.getIsPayzz());
                }
                if (tOrderFee.getFeeId() == 32 && tOrderFee.getMoney() != Double.valueOf(sOrderFee.getZz_f())) {
                    if (tOrderFee.getOrderId() == null) {
                        if (order1.getFlow_status() == 4) {
                            tOrderFee.setAuditStatus(3);
                        } else {
                            tOrderFee.setAuditStatus(1);
                        }
                        tOrderFee.setDetpId(ShiroKit.getUser().getDeptId());
                        tOrderFee.setDotId(ShiroKit.getUser().getDotId());
                        tOrderFee.setOrderId(order.getId());
                        tOrderFee.setFeeIncome(0);
                    }
                    tOrderFee.setCreateUserId(ShiroKit.getUser().getId());
                    tOrderFee.setCreateTime(new Date());
                    tOrderFee.setMoney(Double.valueOf(sOrderFee.getZz_f()) - tOrderFee.getMoney());
                    tOrderFee.setBalance_id(tMonthbalanceDetail.getId());

                    orderFeeWarpper.insert(tOrderFee);


                } else if (tOrderFee.getFeeId() == 43 && tOrderFee.getMoney() != Double.valueOf(sOrderFee.getZz_ld())) {
                    if (tOrderFee.getOrderId() == null) {
                        if (order1.getFlow_status() == 4) {
                            tOrderFee.setAuditStatus(3);
                        } else {
                            tOrderFee.setAuditStatus(1);
                        }
                        tOrderFee.setDetpId(ShiroKit.getUser().getDeptId());
                        tOrderFee.setDotId(ShiroKit.getUser().getDotId());
                        tOrderFee.setOrderId(order.getId());
                        tOrderFee.setFeeIncome(0);
                    }
                    tOrderFee.setCreateUserId(ShiroKit.getUser().getId());
                    tOrderFee.setCreateTime(new Date());
                    tOrderFee.setMoney(Double.valueOf(sOrderFee.getZz_ld()) - tOrderFee.getMoney());
                    tOrderFee.setBalance_id(tMonthbalanceDetail.getId());

                    orderFeeWarpper.insert(tOrderFee);


                } else if (tOrderFee.getFeeId() == 33 && tOrderFee.getMoney() != Double.valueOf(sOrderFee.getZz_ds())) {
                    if (tOrderFee.getOrderId() == null) {
                        if (order1.getFlow_status() == 4) {
                            tOrderFee.setAuditStatus(3);
                        } else {
                            tOrderFee.setAuditStatus(1);
                        }
                        tOrderFee.setDetpId(ShiroKit.getUser().getDeptId());
                        tOrderFee.setDotId(ShiroKit.getUser().getDotId());
                        tOrderFee.setOrderId(order.getId());
                        tOrderFee.setFeeIncome(0);
                    }
                    tOrderFee.setCreateUserId(ShiroKit.getUser().getId());
                    tOrderFee.setBalance_id(tMonthbalanceDetail.getId());
                    tOrderFee.setCreateTime(new Date());
                    tOrderFee.setMoney(Double.valueOf(sOrderFee.getZz_ds()) - tOrderFee.getMoney());
                    orderFeeWarpper.insert(tOrderFee);

                } else if (tOrderFee.getFeeId() == 34 && tOrderFee.getMoney() != Double.valueOf(sOrderFee.getZz_zz())) {
                    if (tOrderFee.getOrderId() == null) {
                        if (order1.getFlow_status() == 4) {
                            tOrderFee.setAuditStatus(3);
                        } else {
                            tOrderFee.setAuditStatus(1);
                        }
                        tOrderFee.setDetpId(ShiroKit.getUser().getDeptId());
                        tOrderFee.setDotId(ShiroKit.getUser().getDotId());
                        tOrderFee.setOrderId(order.getId());
                        tOrderFee.setFeeIncome(0);
                    }
                    tOrderFee.setCreateUserId(ShiroKit.getUser().getId());
                    tOrderFee.setBalance_id(tMonthbalanceDetail.getId());
                    tOrderFee.setCreateTime(new Date());
                    tOrderFee.setMoney(Double.valueOf(sOrderFee.getZz_zz()) - tOrderFee.getMoney());
                    orderFeeWarpper.insert(tOrderFee);


                } else if (tOrderFee.getFeeId() == 35 && tOrderFee.getMoney() != Double.valueOf(sOrderFee.getZz_by())) {
                    if (tOrderFee.getOrderId() == null) {
                        if (order1.getFlow_status() == 4) {
                            tOrderFee.setAuditStatus(3);
                        } else {
                            tOrderFee.setAuditStatus(1);
                        }
                        tOrderFee.setDetpId(ShiroKit.getUser().getDeptId());
                        tOrderFee.setDotId(ShiroKit.getUser().getDotId());
                        tOrderFee.setOrderId(order.getId());
                        tOrderFee.setFeeIncome(0);
                    }
                    tOrderFee.setCreateUserId(ShiroKit.getUser().getId());
                    tOrderFee.setBalance_id(tMonthbalanceDetail.getId());
                    tOrderFee.setCreateTime(new Date());
                    tOrderFee.setMoney(Double.valueOf(sOrderFee.getZz_by()) - tOrderFee.getMoney());
                    orderFeeWarpper.insert(tOrderFee);

                } else if (tOrderFee.getFeeId() == 36 && tOrderFee.getMoney() != Double.valueOf(sOrderFee.getZz_sh())) {
                    if (tOrderFee.getOrderId() == null) {
                        if (order1.getFlow_status() == 4) {
                            tOrderFee.setAuditStatus(3);
                        } else {
                            tOrderFee.setAuditStatus(1);
                        }
                        tOrderFee.setDetpId(ShiroKit.getUser().getDeptId());
                        tOrderFee.setDotId(ShiroKit.getUser().getDotId());
                        tOrderFee.setOrderId(order.getId());
                        tOrderFee.setFeeIncome(0);
                    }
                    tOrderFee.setCreateUserId(ShiroKit.getUser().getId());
                    tOrderFee.setBalance_id(tMonthbalanceDetail.getId());
                    tOrderFee.setCreateTime(new Date());
                    tOrderFee.setMoney(Double.valueOf(sOrderFee.getZz_sh()) - tOrderFee.getMoney());
                    orderFeeWarpper.insert(tOrderFee);

                }
            }
        } else {//更新数据
            for (TOrderFee tOrderFee : orderFees) {
                if (tOrderFee.getFeeId() == 32 && tOrderFee.getMoney() != Double.valueOf(sOrderFee.getZz_f())) {
                    if (tOrderFee.getOrderId() == null) {
                        tOrderFee.setCreateUserId(ShiroKit.getUser().getId());
                        tOrderFee.setCreateTime(new Date());
                        tOrderFee.setMoney(Double.valueOf(sOrderFee.getZz_f()));
                        tOrderFee.setAuditStatus(0);//未结算
                        tOrderFee.setDetpId(ShiroKit.getUser().getDeptId());
                        tOrderFee.setDotId(ShiroKit.getUser().getDotId());
                        tOrderFee.setOrderId(order.getId());
                        tOrderFee.setFeeIncome(0);
                        orderFeeWarpper.insert(tOrderFee);
                    } else {
                        tOrderFee.setCreateUserId(ShiroKit.getUser().getId());
                        tOrderFee.setCreateTime(new Date());
                        tOrderFee.setMoney(Double.valueOf(sOrderFee.getZz_f()));
                        orderFeeWarpper.updateById(tOrderFee);
                    }
                } else if (tOrderFee.getFeeId() == 43 && tOrderFee.getMoney() != Double.valueOf(sOrderFee.getZz_ld())) {
                    //落地费
                    if (tOrderFee.getOrderId() == null) {
                        tOrderFee.setCreateUserId(ShiroKit.getUser().getId());
                        tOrderFee.setCreateTime(new Date());
                        tOrderFee.setMoney(Double.valueOf(sOrderFee.getZz_ld()));
                        tOrderFee.setAuditStatus(0);//未结算
                        tOrderFee.setDetpId(ShiroKit.getUser().getDeptId());
                        tOrderFee.setDotId(ShiroKit.getUser().getDotId());
                        tOrderFee.setOrderId(order.getId());
                        tOrderFee.setFeeIncome(0);
                        orderFeeWarpper.insert(tOrderFee);
                    } else {
                        tOrderFee.setCreateUserId(ShiroKit.getUser().getId());
                        tOrderFee.setCreateTime(new Date());
                        tOrderFee.setMoney(Double.valueOf(sOrderFee.getZz_ld()));
                        orderFeeWarpper.updateById(tOrderFee);
                    }
                } else if (tOrderFee.getFeeId() == 33 && tOrderFee.getMoney() != Double.valueOf(sOrderFee.getZz_ds())) {
                    if (tOrderFee.getOrderId() == null) {
                        tOrderFee.setCreateUserId(ShiroKit.getUser().getId());
                        tOrderFee.setCreateTime(new Date());
                        tOrderFee.setMoney(Double.valueOf(sOrderFee.getZz_ds()));
                        tOrderFee.setAuditStatus(0);//未结算
                        tOrderFee.setDetpId(ShiroKit.getUser().getDeptId());
                        tOrderFee.setDotId(ShiroKit.getUser().getDotId());
                        tOrderFee.setOrderId(order.getId());
                        tOrderFee.setFeeIncome(0);
                        orderFeeWarpper.insert(tOrderFee);
                    } else {
                        tOrderFee.setCreateUserId(ShiroKit.getUser().getId());
                        tOrderFee.setCreateTime(new Date());
                        tOrderFee.setMoney(Double.valueOf(sOrderFee.getZz_ds()));
                        orderFeeWarpper.updateById(tOrderFee);
                    }
                } else if (tOrderFee.getFeeId() == 36 && tOrderFee.getMoney() != Double.valueOf(sOrderFee.getZz_sh())) {
                    if (tOrderFee.getOrderId() == null) {
                        tOrderFee.setCreateUserId(ShiroKit.getUser().getId());
                        tOrderFee.setCreateTime(new Date());
                        tOrderFee.setMoney(Double.valueOf(sOrderFee.getZz_sh()));
                        tOrderFee.setAuditStatus(0);//未结算
                        tOrderFee.setDetpId(ShiroKit.getUser().getDeptId());
                        tOrderFee.setDotId(ShiroKit.getUser().getDotId());
                        tOrderFee.setOrderId(order.getId());
                        tOrderFee.setFeeIncome(0);
                        orderFeeWarpper.insert(tOrderFee);
                    } else {
                        tOrderFee.setCreateUserId(ShiroKit.getUser().getId());
                        tOrderFee.setCreateTime(new Date());
                        tOrderFee.setMoney(Double.valueOf(sOrderFee.getZz_sh()));
                        orderFeeWarpper.updateById(tOrderFee);
                    }
                } else if (tOrderFee.getFeeId() == 35 && tOrderFee.getMoney() != Double.valueOf(sOrderFee.getZz_by())) {
                    if (tOrderFee.getOrderId() == null) {
                        tOrderFee.setCreateUserId(ShiroKit.getUser().getId());
                        tOrderFee.setCreateTime(new Date());
                        tOrderFee.setMoney(Double.valueOf(sOrderFee.getZz_by()));
                        tOrderFee.setAuditStatus(0);//未结算
                        tOrderFee.setDetpId(ShiroKit.getUser().getDeptId());
                        tOrderFee.setDotId(ShiroKit.getUser().getDotId());
                        tOrderFee.setOrderId(order.getId());
                        tOrderFee.setFeeIncome(0);
                        orderFeeWarpper.insert(tOrderFee);
                    } else {
                        tOrderFee.setCreateUserId(ShiroKit.getUser().getId());
                        tOrderFee.setCreateTime(new Date());
                        tOrderFee.setMoney(Double.valueOf(sOrderFee.getZz_by()));
                        orderFeeWarpper.updateById(tOrderFee);
                    }
                } else if (tOrderFee.getFeeId() == 34 && tOrderFee.getMoney() != Double.valueOf(sOrderFee.getZz_zz())) {
                    if (tOrderFee.getOrderId() == null) {
                        tOrderFee.setCreateUserId(ShiroKit.getUser().getId());
                        tOrderFee.setCreateTime(new Date());
                        tOrderFee.setMoney(Double.valueOf(sOrderFee.getZz_zz()));
                        tOrderFee.setAuditStatus(0);//未结算
                        tOrderFee.setDetpId(ShiroKit.getUser().getDeptId());
                        tOrderFee.setDotId(ShiroKit.getUser().getDotId());
                        tOrderFee.setOrderId(order.getId());
                        tOrderFee.setFeeIncome(0);
                        orderFeeWarpper.insert(tOrderFee);
                    } else {
                        tOrderFee.setCreateUserId(ShiroKit.getUser().getId());
                        tOrderFee.setCreateTime(new Date());
                        tOrderFee.setMoney(Double.valueOf(sOrderFee.getZz_zz()));
                        orderFeeWarpper.updateById(tOrderFee);
                    }
                }
            }

        }
        if(receive_id!=null){
            OrderBackRecord orderReceive=new OrderBackRecord();
            orderReceive.setId(receive_id);
            if(receive_time!=null&&!receive_time.isEmpty()){
                orderReceive.setReceiveTime(DateUtil.parseDate(receive_time));
            }
            orderReceive.setRemark(remark);
            orderReceive.setReceiver(receiver);
            orderReceive.setEdit_time(new Date());
            orderReceive.setEdit_user(ShiroKit.getUser().getId());
            backRecordMapper.updateById(orderReceive);
        }
        if(send_id!=null){
            OrderBackRecord orderSend=new OrderBackRecord();
            orderSend.setId(send_id);
            if(send_Time!=null&&!send_Time.isEmpty()){
                orderSend.setSendTime(DateUtil.parseDate(send_Time));
            }
            orderSend.setRemark(remark);
            orderSend.setReceiver(sender);
            orderSend.setProcessMethod(processMethod);
            orderSend.setEdit_time(new Date());
            orderSend.setEdit_user(ShiroKit.getUser().getId());
            backRecordMapper.updateById(orderSend);
        }

        order.setEdit_user(ShiroKit.getUser().getId());
        order.setEdit_time(new Date());
        orderMapper.updateById(order);

        return super.SUCCESS_TIP;
    }

    /**
     * 修改订单类型
     */
    @RequestMapping(value = "/changeOrderType")
    @ResponseBody
    @Transactional
    public Object changeOrderType(TOrder order,   String  send_Time,String  receive_time,String sender,String receiver,Integer send_id,Integer processMethod,Integer receive_id,String remark,  SOrderFee sOrderFee) {
        TOrder order1 = orderMapper.selectById(order.getId());
//        EntityWrapper<TOrderFee> entityWrapper = new EntityWrapper<>();
//        entityWrapper.where("order_id={0}", order.getId());
//        List<TOrderFee> feeOrders = itOrderFeeService.selectList(entityWrapper);
        List<Integer> mIds = new ArrayList<>();//上家的费用
        mIds.add(22);
        mIds.add(23);
        mIds.add(24);
        mIds.add(25);
        mIds.add(26);
        List<TOrderFee> mOrderFees = orderFeeWarpper.selectOrderFee(order1.getId() + "", mIds);
        Map param3=new HashMap();
        param3.put("dot_id",ShiroKit.getUser().getDotId());
        param3.put("nType", Const.NTYPE_04); // 类别 00 初始 01 配送 02 中转 03 自提  04 月结
        TMonthbalanceDetail sMonthbalanceDetail = new TMonthbalanceDetail();
        sMonthbalanceDetail.setCreater(ShiroKit.getUser().getId());
        sMonthbalanceDetail.setCreatetime(new Date());
        sMonthbalanceDetail.setDotId(order1.getDotId());
        sMonthbalanceDetail.setIncome(5);
        sMonthbalanceDetail.setIncomeId(order1.getId());
        sMonthbalanceDetail.setIncomeName(order1.getOriginal_num());
        if (sOrderFee.getIsPay().equals("0")) {
            sMonthbalanceDetail.setSettlementType(0);
            sMonthbalanceDetail.setStatus("1");
        } else {
            sMonthbalanceDetail.setSettlementType(1);
            sMonthbalanceDetail.setStatus("0");
        }

        sMonthbalanceDetail.setMoney(Double.valueOf(sOrderFee.getA1()));

        TBill sBill = new TBill();
        if (sOrderFee.getIsPay().equals("0")) {
            String billNum = "TT" + ShiroKit.getUser().getId() + "_" + DateUtil.getAllTimes();
            sBill.setDotId(order1.getDotId());
            sBill.setCreateTime(new Date());
            sBill.setBillNum(billNum);
            sBill.setCash(Float.valueOf(sOrderFee.getCashs()));
            sBill.setBank(Float.valueOf(sOrderFee.getBanks()));
            sBill.setAlipay(Float.valueOf(sOrderFee.getAlipays()));
            sBill.setWeixin(Float.valueOf(sOrderFee.getWeixins()));
            sBill.setEditUser(ShiroKit.getUser().getId());
            if(!("0".equals(sOrderFee.getCashs())&&"0".equals(sOrderFee.getBanks())&&"0".equals(sOrderFee.getAlipays())&&"0".equals(sOrderFee.getWeixins()))){
                billMapper.insert(sBill);
            }
        }
        sMonthbalanceDetail.setBillId(sBill.getId());
        sMonthbalanceDetail.setTraner(order1.getSendDeptName());
        sMonthbalanceDetail.setBalance_num(tDictMapper.getNumCode(param3));
        if("0".equals(sOrderFee.getA1())){
            sMonthbalanceDetail.setStatus(OrderEnum.MONTHBALANCE_STATUS_SETTLED.getCode()+"");
        }
        monthbalanceDetailMapper.insert(sMonthbalanceDetail);

        if (order1.getFlow_status() > 0) {
            for (TOrderFee tOrderFee : mOrderFees) {
                tOrderFee.setId(null);
                if (tOrderFee.getFeeId() == 22 && tOrderFee.getMoney() != Double.valueOf(sOrderFee.getS_tf())) {
                    tOrderFee.setCreateUserId(ShiroKit.getUser().getId());
                    tOrderFee.setCreateTime(new Date());

                    tOrderFee.setBalance_id(sMonthbalanceDetail.getId());
                    if (Double.valueOf(sOrderFee.getS_tf()) - tOrderFee.getMoney() != 0) {
                        tOrderFee.setMoney(Double.valueOf(sOrderFee.getS_tf()) - tOrderFee.getMoney());
                        if (tOrderFee.getOrderId() == null) {
                            if (sOrderFee.getIsPay().equals("0")) {//现结
                                tOrderFee.setSettlementType("0");
                            } else {
                                tOrderFee.setSettlementType("1");

                            }
                            if (order1.getFlow_status() == 4) {
                                tOrderFee.setAuditStatus(3);
                            } else {
                                tOrderFee.setAuditStatus(1);
                            }
                            tOrderFee.setDetpId(ShiroKit.getUser().getDeptId());
                            tOrderFee.setDotId(ShiroKit.getUser().getDotId());
                            tOrderFee.setOrderId(order.getId());
                            tOrderFee.setFeeIncome(0);
                        }
                        orderFeeWarpper.insert(tOrderFee);
                    }

                } else if (tOrderFee.getFeeId() == 23 && tOrderFee.getMoney() != Double.valueOf(sOrderFee.getS_ds())) {
                    tOrderFee.setCreateUserId(ShiroKit.getUser().getId());
                    tOrderFee.setBalance_id(sMonthbalanceDetail.getId());
                    tOrderFee.setCreateTime(new Date());
                    if (Double.valueOf(sOrderFee.getS_ds()) - tOrderFee.getMoney() != 0) {
                        tOrderFee.setMoney(Double.valueOf(sOrderFee.getS_ds()) - tOrderFee.getMoney());
                        if (tOrderFee.getOrderId() == null) {
                            if (sOrderFee.getIsPay().equals("0")) {//现结
                                tOrderFee.setSettlementType("0");
                            } else {
                                tOrderFee.setSettlementType("1");

                            }
                            if (order1.getFlow_status() == 4) {
                                tOrderFee.setAuditStatus(3);
                            } else {
                                tOrderFee.setAuditStatus(1);
                            }
                            tOrderFee.setDetpId(ShiroKit.getUser().getDeptId());
                            tOrderFee.setDotId(ShiroKit.getUser().getDotId());
                            tOrderFee.setOrderId(order.getId());
                            tOrderFee.setFeeIncome(0);
                        }
                        orderFeeWarpper.insert(tOrderFee);
                    }


                } else if (tOrderFee.getFeeId() == 26 && tOrderFee.getMoney() != Double.valueOf(sOrderFee.getS_by())) {
                    tOrderFee.setCreateUserId(ShiroKit.getUser().getId());
                    tOrderFee.setBalance_id(sMonthbalanceDetail.getId());
                    tOrderFee.setCreateTime(new Date());
                    if (Double.valueOf(sOrderFee.getS_by()) - tOrderFee.getMoney() != 0) {
                        tOrderFee.setMoney(Double.valueOf(sOrderFee.getS_by()) - tOrderFee.getMoney());
                        if (tOrderFee.getOrderId() == null) {
                            if (sOrderFee.getIsPay().equals("0")) {//现结
                                tOrderFee.setSettlementType("0");
                            } else {
                                tOrderFee.setSettlementType("1");

                            }
                            if (order1.getFlow_status() == 4) {
                                tOrderFee.setAuditStatus(3);
                            } else {
                                tOrderFee.setAuditStatus(1);
                            }
                            tOrderFee.setDetpId(ShiroKit.getUser().getDeptId());
                            tOrderFee.setDotId(ShiroKit.getUser().getDotId());
                            tOrderFee.setOrderId(order.getId());
                            tOrderFee.setFeeIncome(0);
                        }
                        orderFeeWarpper.insert(tOrderFee);
                    }

                } else if (tOrderFee.getFeeId() == 25 && tOrderFee.getMoney() != Double.valueOf(sOrderFee.getS_sh())) {
                    tOrderFee.setCreateUserId(ShiroKit.getUser().getId());
                    tOrderFee.setBalance_id(sMonthbalanceDetail.getId());
                    tOrderFee.setCreateTime(new Date());
                    if (Double.valueOf(sOrderFee.getS_sh()) - tOrderFee.getMoney() != 0) {
                        tOrderFee.setMoney(Double.valueOf(sOrderFee.getS_sh()) - tOrderFee.getMoney());
                        if (tOrderFee.getOrderId() == null) {
                            if (sOrderFee.getIsPay().equals("0")) {//现结
                                tOrderFee.setSettlementType("0");
                            } else {
                                tOrderFee.setSettlementType("1");

                            }
                            if (order1.getFlow_status() == 4) {
                                tOrderFee.setAuditStatus(3);
                            } else {
                                tOrderFee.setAuditStatus(1);
                            }
                            tOrderFee.setDetpId(ShiroKit.getUser().getDeptId());
                            tOrderFee.setDotId(ShiroKit.getUser().getDotId());
                            tOrderFee.setOrderId(order.getId());
                            tOrderFee.setFeeIncome(0);
                        }
                        orderFeeWarpper.insert(tOrderFee);
                    }

                } else if (tOrderFee.getFeeId() == 24 && tOrderFee.getMoney() != Double.valueOf(sOrderFee.getS_zz())) {
                    tOrderFee.setCreateUserId(ShiroKit.getUser().getId());
                    tOrderFee.setBalance_id(sMonthbalanceDetail.getId());
                    tOrderFee.setCreateTime(new Date());
                    if (Double.valueOf(sOrderFee.getS_zz()) - tOrderFee.getMoney() != 0) {
                        tOrderFee.setMoney(Double.valueOf(sOrderFee.getS_zz()) - tOrderFee.getMoney());
                        if (tOrderFee.getOrderId() == null) {
                            if (sOrderFee.getIsPay().equals("0")) {//现结
                                tOrderFee.setSettlementType("0");
                            } else {
                                tOrderFee.setSettlementType("1");

                            }
                            if (order1.getFlow_status() == 4) {
                                tOrderFee.setAuditStatus(3);
                            } else {
                                tOrderFee.setAuditStatus(1);
                            }
                            tOrderFee.setDetpId(ShiroKit.getUser().getDeptId());
                            tOrderFee.setDotId(ShiroKit.getUser().getDotId());
                            tOrderFee.setOrderId(order.getId());
                            tOrderFee.setFeeIncome(0);
                        }
                        orderFeeWarpper.insert(tOrderFee);
                    }
                }

//                if (sOrderFee.getIsPay().equals("0") && tOrderFee.getId() != null) {
//                    Map param = new HashMap();
//                    param.put("order_fee_id", tOrderFee.getId() + "");
//                    param.put("order_id", tOrderFee.getOrderId() + "");
//                    param.put("bill_id", sBill.getId());
//                    tTrainMapper.addFeeBill(param);
//                }
            }

        } else {


            for (TOrderFee tOrderFee : mOrderFees) {

                if (tOrderFee.getFeeId() == 22 && tOrderFee.getMoney() != Double.valueOf(sOrderFee.getS_tf())) {
                    if (tOrderFee.getOrderId() == null) {
                        tOrderFee.setCreateUserId(ShiroKit.getUser().getId());
                        tOrderFee.setCreateTime(new Date());
                        tOrderFee.setMoney(Double.valueOf(sOrderFee.getS_tf()));
                        tOrderFee.setAuditStatus(0);//未结算
                        tOrderFee.setDetpId(ShiroKit.getUser().getDeptId());
                        tOrderFee.setDotId(ShiroKit.getUser().getDotId());
                        tOrderFee.setOrderId(order.getId());
                        tOrderFee.setFeeIncome(0);
                        orderFeeWarpper.insert(tOrderFee);
                    } else {
                        tOrderFee.setCreateUserId(ShiroKit.getUser().getId());
                        tOrderFee.setCreateTime(new Date());
                        tOrderFee.setMoney(Double.valueOf(sOrderFee.getS_tf()));
                        orderFeeWarpper.updateById(tOrderFee);
                    }
                } else if (tOrderFee.getFeeId() == 23 && tOrderFee.getMoney() != Double.valueOf(sOrderFee.getS_ds())) {
                    if (tOrderFee.getOrderId() == null) {
                        tOrderFee.setCreateUserId(ShiroKit.getUser().getId());
                        tOrderFee.setCreateTime(new Date());
                        tOrderFee.setMoney(Double.valueOf(sOrderFee.getS_ds()));
                        tOrderFee.setAuditStatus(0);//未结算
                        tOrderFee.setDetpId(ShiroKit.getUser().getDeptId());
                        tOrderFee.setDotId(ShiroKit.getUser().getDotId());
                        tOrderFee.setOrderId(order.getId());
                        tOrderFee.setFeeIncome(0);
                        orderFeeWarpper.insert(tOrderFee);
                    } else {
                        tOrderFee.setCreateUserId(ShiroKit.getUser().getId());
                        tOrderFee.setCreateTime(new Date());
                        tOrderFee.setMoney(Double.valueOf(sOrderFee.getS_ds()));
                        orderFeeWarpper.updateById(tOrderFee);
                    }
                } else if (tOrderFee.getFeeId() == 26 && tOrderFee.getMoney() != Double.valueOf(sOrderFee.getS_by())) {
                    if (tOrderFee.getOrderId() == null) {
                        tOrderFee.setCreateUserId(ShiroKit.getUser().getId());
                        tOrderFee.setCreateTime(new Date());
                        tOrderFee.setMoney(Double.valueOf(sOrderFee.getS_by()));
                        tOrderFee.setAuditStatus(0);//未结算
                        tOrderFee.setDetpId(ShiroKit.getUser().getDeptId());
                        tOrderFee.setDotId(ShiroKit.getUser().getDotId());
                        tOrderFee.setOrderId(order.getId());
                        tOrderFee.setFeeIncome(0);
                        orderFeeWarpper.insert(tOrderFee);
                    } else {
                        tOrderFee.setCreateUserId(ShiroKit.getUser().getId());
                        tOrderFee.setCreateTime(new Date());
                        tOrderFee.setMoney(Double.valueOf(sOrderFee.getS_by()));
                        orderFeeWarpper.updateById(tOrderFee);
                    }
                } else if (tOrderFee.getFeeId() == 25 && tOrderFee.getMoney() != Double.valueOf(sOrderFee.getS_sh())) {
                    if (tOrderFee.getOrderId() == null) {
                        tOrderFee.setCreateUserId(ShiroKit.getUser().getId());
                        tOrderFee.setCreateTime(new Date());
                        tOrderFee.setMoney(Double.valueOf(sOrderFee.getS_sh()));
                        tOrderFee.setAuditStatus(0);//未结算
                        tOrderFee.setDetpId(ShiroKit.getUser().getDeptId());
                        tOrderFee.setDotId(ShiroKit.getUser().getDotId());
                        tOrderFee.setOrderId(order.getId());
                        tOrderFee.setFeeIncome(0);
                        orderFeeWarpper.insert(tOrderFee);
                    } else {
                        tOrderFee.setCreateUserId(ShiroKit.getUser().getId());
                        tOrderFee.setCreateTime(new Date());
                        tOrderFee.setMoney(Double.valueOf(sOrderFee.getS_sh()));
                        orderFeeWarpper.updateById(tOrderFee);
                    }
                } else if (tOrderFee.getFeeId() == 24 && tOrderFee.getMoney() != Double.valueOf(sOrderFee.getS_zz())) {
                    if (tOrderFee.getOrderId() == null) {
                        tOrderFee.setCreateUserId(ShiroKit.getUser().getId());
                        tOrderFee.setCreateTime(new Date());
                        tOrderFee.setMoney(Double.valueOf(sOrderFee.getS_zz()));
                        tOrderFee.setAuditStatus(0);//未结算
                        tOrderFee.setDetpId(ShiroKit.getUser().getDeptId());
                        tOrderFee.setDotId(ShiroKit.getUser().getDotId());
                        tOrderFee.setOrderId(order.getId());
                        tOrderFee.setFeeIncome(0);
                        orderFeeWarpper.insert(tOrderFee);
                    } else {
                        tOrderFee.setCreateUserId(ShiroKit.getUser().getId());
                        tOrderFee.setCreateTime(new Date());
                        tOrderFee.setMoney(Double.valueOf(sOrderFee.getS_zz()));
                        orderFeeWarpper.updateById(tOrderFee);
                    }
                }
            }
        }
        String log_no = DateUtil.getAllTimes();

        if ("1".equals(order1.getIsTransfer())) {
            //原来为中转单，则删除与与下家的费用重新生成新类型的费用
            orderMapper.addOrderFeeLogByIdAndType(log_no, ShiroKit.getUser().getId() + "", order1.getId(), "6");
            orderMapper.delOrderFeeIdAndType(order1.getId(), "6");
        } else {
            orderMapper.addOrderFeeLogByIdAndType(log_no, ShiroKit.getUser().getId() + "", order1.getId(), "3");
            orderMapper.delOrderFeeIdAndType(order1.getId(), "3");
        }


        //重新生成费用

        List<Map> mapList = new ArrayList<>();
        if ("2".equals(sOrderFee.getModeType())) {
            //用户新修改的类型为 中转
            Map map_fz = new HashMap();
            map_fz.put("order_id", order.getId());
            map_fz.put("detp_id", ShiroKit.getUser().getDeptId());
            map_fz.put("dot_id", ShiroKit.getUser().getDotId());
            map_fz.put("create_user_id", ShiroKit.getUser().getId());
            map_fz.put("audit_status", "0");
            map_fz.put("fee_id", "32");
            map_fz.put("money", sOrderFee.getZz_f() == null ? 0 : sOrderFee.getZz_f());
            map_fz.put("fee_Income", "1");

            Map map_cz = new HashMap();
            map_cz.put("order_id", order.getId());
            map_cz.put("detp_id", ShiroKit.getUser().getDeptId());
            map_cz.put("dot_id", ShiroKit.getUser().getDotId());
            map_cz.put("create_user_id", ShiroKit.getUser().getId());
            map_cz.put("audit_status", "0");
            map_cz.put("fee_id", "33");
            map_cz.put("money", sOrderFee.getZz_ds() == null ? 0 : sOrderFee.getZz_ds());
            map_cz.put("fee_Income", "1");


            Map map3 = new HashMap();
            map3.put("order_id", order.getId());
            map3.put("detp_id", ShiroKit.getUser().getDeptId());
            map3.put("dot_id", ShiroKit.getUser().getDotId());
            map3.put("create_user_id", ShiroKit.getUser().getId());
            map3.put("audit_status", "0");
            map3.put("fee_id", "34");
            map3.put("money", sOrderFee.getZz_zz() == null ? 0 : sOrderFee.getZz_zz());
            map3.put("fee_Income", "1");

            Map map4 = new HashMap();
            map4.put("order_id", order.getId());
            map4.put("detp_id", ShiroKit.getUser().getDeptId());
            map4.put("dot_id", ShiroKit.getUser().getDotId());
            map4.put("create_user_id", ShiroKit.getUser().getId());
            map4.put("audit_status", "0");
            map4.put("fee_id", "35");
            map4.put("money", sOrderFee.getZz_by() == null ? 0 : sOrderFee.getZz_by());
            map4.put("fee_Income", "1");


            Map map5 = new HashMap();
            map5.put("order_id", order.getId());
            map5.put("detp_id", ShiroKit.getUser().getDeptId());
            map5.put("dot_id", ShiroKit.getUser().getDotId());
            map5.put("create_user_id", ShiroKit.getUser().getId());
            map5.put("audit_status", "0");
            map5.put("fee_id", "36");
            map5.put("money", sOrderFee.getZz_sh() == null ? 0 : sOrderFee.getZz_sh());
            map5.put("fee_Income", "1");

            Map map6 = new HashMap();
            map6.put("order_id", order.getId());
            map6.put("detp_id", ShiroKit.getUser().getDeptId());
            map6.put("dot_id", ShiroKit.getUser().getDotId());
            map6.put("create_user_id", ShiroKit.getUser().getId());
            map6.put("audit_status", "0");
            map6.put("fee_id", "43");
            map6.put("money", sOrderFee.getZz_ld() == null ? 0 : sOrderFee.getZz_ld());
            map6.put("fee_Income", "1");
            mapList.add(map3);
            mapList.add(map4);
            mapList.add(map5);
            mapList.add(map6);
            mapList.add(map_cz);
            mapList.add(map_fz);
        } else {

            Map mapC = new HashMap();
            if ("0".equals(sOrderFee.getModeType())) {//自提
                mapC.put("order_id", order.getId());
                mapC.put("detp_id", ShiroKit.getUser().getDeptId());
                mapC.put("dot_id", ShiroKit.getUser().getDotId());
                mapC.put("create_user_id", ShiroKit.getUser().getId());
                mapC.put("audit_status", "0");

                mapC.put("fee_id", "2");
                mapC.put("money", sOrderFee.getZ_ds() == null ? 0 : sOrderFee.getZ_ds());
                mapC.put("fee_Income", "0");
            } else {
                //配送
                mapC.put("order_id", order.getId());
                mapC.put("detp_id", ShiroKit.getUser().getDeptId());
                mapC.put("dot_id", ShiroKit.getUser().getDotId());
                mapC.put("create_user_id", ShiroKit.getUser().getId());
                mapC.put("audit_status", "0");

                mapC.put("fee_id", "38");
                mapC.put("money", sOrderFee.getP_ds() == null ? 0 : sOrderFee.getP_ds());
                mapC.put("fee_Income", "0");
            }

            mapList.add(mapC);
            if ("0".equals(sOrderFee.getModeType())) {
                Map map = new HashMap();
                map.put("order_id", order.getId());
                map.put("detp_id", ShiroKit.getUser().getDeptId());
                map.put("dot_id", ShiroKit.getUser().getDotId());
                map.put("create_user_id", ShiroKit.getUser().getId());
                map.put("audit_status", "0");
                map.put("fee_id", "1");
                map.put("money", sOrderFee.getZ_tf() == null ? 0 : sOrderFee.getZ_tf());
                map.put("fee_Income", "0");

                Map mapr = new HashMap();

                mapr.put("order_id", order.getId());
                mapr.put("detp_id", ShiroKit.getUser().getDeptId());
                mapr.put("dot_id", ShiroKit.getUser().getDotId());
                mapr.put("create_user_id", ShiroKit.getUser().getId());
                mapr.put("audit_status", "0");
                mapr.put("fee_id", "39");
                mapr.put("money", sOrderFee.getZ_by() == null ? 0 : sOrderFee.getZ_by());
                mapr.put("fee_Income", "0");

                Map mapb = new HashMap();

                mapb.put("order_id", order.getId());
                mapb.put("detp_id", ShiroKit.getUser().getDeptId());
                mapb.put("dot_id", ShiroKit.getUser().getDotId());
                mapb.put("create_user_id", ShiroKit.getUser().getId());
                mapb.put("audit_status", "0");
                mapb.put("fee_id", "40");
                mapb.put("money", sOrderFee.getZ_cc() == null ? 0 : sOrderFee.getZ_cc());
                mapb.put("fee_Income", "0");

                Map mapl = new HashMap();

                mapl.put("order_id", order.getId());
                mapl.put("detp_id", ShiroKit.getUser().getDeptId());
                mapl.put("dot_id", ShiroKit.getUser().getDotId());
                mapl.put("create_user_id", ShiroKit.getUser().getId());
                mapl.put("audit_status", "0");
                mapl.put("fee_id", "41");
                mapl.put("money", sOrderFee.getZ_ld() == null ? 0 : sOrderFee.getZ_ld());
                mapl.put("fee_Income", "0");

                mapList.add(map);
                mapList.add(mapb);
                mapList.add(mapr);
                mapList.add(mapl);

            } else {
                Map mapA = new HashMap();
                mapA.put("order_id", order.getId());
                mapA.put("detp_id", ShiroKit.getUser().getDeptId());
                mapA.put("dot_id", ShiroKit.getUser().getDotId());
                mapA.put("create_user_id", ShiroKit.getUser().getId());
                mapA.put("audit_status", "0");
                mapA.put("fee_id", "37");
                mapA.put("money", sOrderFee.getP_tf() == null ? 0 : sOrderFee.getP_tf());
                mapA.put("fee_Income", "0");
                Map mapr = new HashMap();
                mapr.put("order_id", order.getId());
                mapr.put("detp_id", ShiroKit.getUser().getDeptId());
                mapr.put("dot_id", ShiroKit.getUser().getDotId());
                mapr.put("create_user_id", ShiroKit.getUser().getId());
                mapr.put("audit_status", "0");
                mapr.put("fee_id", "27");
                mapr.put("money", sOrderFee.getP_sh() == null ? 0 : sOrderFee.getP_sh());
                mapr.put("fee_Income", "0");

                Map mapb = new HashMap();

                mapb.put("order_id", order.getId());
                mapb.put("detp_id", ShiroKit.getUser().getDeptId());
                mapb.put("dot_id", ShiroKit.getUser().getDotId());
                mapb.put("create_user_id", ShiroKit.getUser().getId());
                mapb.put("audit_status", "0");
                mapb.put("fee_id", "28");
                mapb.put("money", sOrderFee.getP_by() == null ? 0 : sOrderFee.getP_by());
                mapb.put("fee_Income", "0");

                Map map1 = new HashMap();

                map1.put("order_id", order.getId());
                map1.put("detp_id", ShiroKit.getUser().getDeptId());
                map1.put("dot_id", ShiroKit.getUser().getDotId());
                map1.put("create_user_id", ShiroKit.getUser().getId());
                map1.put("audit_status", "0");
                map1.put("fee_id", "29");
                map1.put("money", sOrderFee.getP_cc() == null ? 0 : sOrderFee.getP_cc());
                map1.put("fee_Income", "0");

                Map map2 = new HashMap();

                map2.put("order_id", order.getId());
                map2.put("detp_id", ShiroKit.getUser().getDeptId());
                map2.put("dot_id", ShiroKit.getUser().getDotId());
                map2.put("create_user_id", ShiroKit.getUser().getId());
                map2.put("audit_status", "0");
                map2.put("fee_id", "30");
                map2.put("money", sOrderFee.getP_cf() == null ? 0 : sOrderFee.getP_cf());
                map2.put("fee_Income", "1");

                Map map3 = new HashMap();

                map3.put("order_id", order.getId());
                map3.put("detp_id", ShiroKit.getUser().getDeptId());
                map3.put("dot_id", ShiroKit.getUser().getDotId());
                map3.put("create_user_id", ShiroKit.getUser().getId());
                map3.put("audit_status", "0");
                map3.put("fee_id", "42");
                map3.put("money", sOrderFee.getP_ld() == null ? 0 : sOrderFee.getP_ld());
                map3.put("fee_Income", "1");


                mapList.add(map1);
                mapList.add(mapA);
                mapList.add(map2);
                mapList.add(map3);
                mapList.add(mapb);
                mapList.add(mapr);

            }
        }

        if (mapList.size() > 0) {
            int adds = orderMapper.addOrderFee(mapList);
        }


//        orderBackRecord.setId(null);
//        if (!StringUtils.isEmpty(getPara("send_Time"))) {
//            orderBackRecord.setSendTime(DateUtil.parseTime(getPara("send_Time")));
//        }
//        orderBackRecord.setEdit_time(new Date());
//        orderBackRecord.setType(1);
//        orderBackRecord.setEdit_user(ShiroKit.getUser().getId());
//        if (!StringUtils.isEmpty(getPara("bacrRecord"))) {
//            int bacrRecordid = Integer.valueOf(getPara("bacrRecord"));
//            orderBackRecord.setId(bacrRecordid);
//        }
//
//        if (orderBackRecord.getId() == null) {
//            backRecordMapper.insert(orderBackRecord);
//            order.setOrder_back_receive_id(orderBackRecord.getId());
//        } else {
//            backRecordMapper.updateById(orderBackRecord);
//            order.setOrder_back_receive_id(orderBackRecord.getId());
//        }
//        if (orderBackRecord.getProcessMethod() != null && orderBackRecord.getProcessMethod() != 0) {
//            order.setReceiptStatus(2);
//        }

        if(receive_id!=null){
            OrderBackRecord orderReceive=new OrderBackRecord();
            orderReceive.setId(receive_id);
            if(receive_time!=null&&!receive_time.isEmpty()){
                orderReceive.setReceiveTime(DateUtil.parseDate(receive_time));
            }
            orderReceive.setRemark(remark);
            orderReceive.setReceiver(receiver);
            orderReceive.setEdit_time(new Date());
            orderReceive.setEdit_user(ShiroKit.getUser().getId());
            backRecordMapper.updateById(orderReceive);
        }
        if(send_id!=null){
            OrderBackRecord orderSend=new OrderBackRecord();
            orderSend.setId(send_id);
            if(send_Time!=null&&!send_Time.isEmpty()){
                orderSend.setSendTime(DateUtil.parseDate(send_Time));
            }
            orderSend.setRemark(remark);
            orderSend.setReceiver(sender);
            orderSend.setProcessMethod(processMethod);
            orderSend.setEdit_time(new Date());
            orderSend.setEdit_user(ShiroKit.getUser().getId());
            backRecordMapper.updateById(orderSend);
        }

        order.setEdit_user(ShiroKit.getUser().getId());
        order.setEdit_time(new Date());
        if ("2".equals(sOrderFee.getModeType())) {
            order.setIsTransfer("1");
        } else if ("0".equals(sOrderFee.getModeType())) {
            order.setIsTransfer("0");
            order.setMode(0);
        } else if ("1".equals(sOrderFee.getModeType())) {
            order.setIsTransfer("0");
            order.setMode(1);
        }
        orderMapper.updateById(order);

        return super.SUCCESS_TIP;
    }


    /**
     * 修改订单管理
     */
    @RequestMapping(value = "/transferOrderFee")
    @ResponseBody
    public Object transferOrderFee(String str) {

        JSONArray jsonArray = JSONArray.parseArray(str);
        String no = DateUtil.getMsTime();
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            String order_id = jsonObject.getString("order_id");
            String tran_name = jsonObject.getString("tran_name");
            String reallyCard = jsonObject.getString("reallyCard");
            String reallyUser = jsonObject.getString("reallyUser");
            String car_no = jsonObject.getString("car_no");
            String driverId = jsonObject.getString("driverId");
            List<Map> maps = jsonObject.getJSONArray("fees").toJavaList(Map.class);
            tOrderService.opOrderFee(maps, ShiroKit.getUser().getDeptId(), ShiroKit.getUser().getId(), no, order_id);
            TOrder order = new TOrder();
            order.setId(Integer.valueOf(order_id));
            order.setTransfer_name(tran_name);
            order.setStatus(2);
            order.setReallyCard(reallyCard);
            order.setReallyUser(reallyUser);
            if (!StringUtils.isEmpty(driverId)) {
                order.setDriverId(Integer.valueOf(driverId));
            }
            tOrderService.updateById(order);
        }


        return super.SUCCESS_TIP;
    }

    @RequestMapping(value = "/disOrderFee")
    @ResponseBody
    public Object disOrderFee(String str) {

        JSONArray jsonArray = JSONArray.parseArray(str);
        String no = DateUtil.getMsTime();
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            String order_id = jsonObject.getString("order_id");
            String tran_name = jsonObject.getString("tran_name");
            List<Map> maps = jsonObject.getJSONArray("fees").toJavaList(Map.class);
            tOrderService.opOrderFee(maps, ShiroKit.getUser().getDeptId(), ShiroKit.getUser().getId(), no, order_id);
            TOrder order = new TOrder();
            order.setId(Integer.valueOf(order_id));
            order.setReallyUser(tran_name);
            order.setStatus(2);
            tOrderService.updateById(order);
        }


        return super.SUCCESS_TIP;
    }

    /**
     * 订单管理详情
     */
    @RequestMapping(value = "/detail/{tOrderId}")
    @ResponseBody
    public Object detail(@PathVariable("tOrderId") Integer tOrderId) {
        return tOrderService.selectById(tOrderId);
    }

    @RequestMapping(value = "/smsCallBack")
    public void smsCallBack(String sms_status) {

        JSONArray jsonArray = JSONArray.parseArray(sms_status);
        JSONObject jsonObject = jsonArray.getJSONObject(0);
        String uuid = jsonObject.getString("uid");
        String type = uuid.split("|")[1];
        String orderNum = uuid.split("|")[0];
        String id = uuid.split("|")[2];
        TOrder order = tOrderService.selectById(Integer.valueOf(id));
        int detp_id = dotMapper.selectById(order.getDotId()).getDeptid();
        Map map1 = new HashMap();
        map1.put("dept_id", detp_id);
        if (jsonObject.getString("report_status").equals("SUCCESS")) {
            order.setCall_str("通知成功");
        } else {
            YunpianConf yunpianConf = new YunpianConf().build();
            YunpianClient yp = new YunpianClient().init();
            if (type.equals("1")) {
                Map<String, String> params = new HashMap<String, String>();
                params.put("apikey", yunpianConf.getApikey());
                params.put("mobile", jsonObject.getString("mobile"));
                params.put("tpl_id", "2175074");
                params.put("tpl_value", order.getPush_str());
                params.put("callback_url", "");
                params.put("uid", orderNum + "|" + 0 + "|" + id);
                Result<VoiceSend> sendResult = yp.voice().send(params);
                if (sendResult.getCode() != 0) {
                    order.setCall_str(sendResult.getMsg());
                    deptMapper.smsCall(map1);
                }

            } else if (type.equals("2")) {

                Map<String, String> params = new HashMap<String, String>();
                params.put("apikey", yunpianConf.getApikey());
                params.put("text", order.getPush_str());
                params.put("mobile", jsonObject.getString("mobile"));
                params.put("uid", orderNum + "|" + 0 + "|" + id);
                params.put("callback_url", "");
                Result<SmsSingleSend> sendResult = yp.sms().single_send(params);
                if (sendResult.getCode() != 0) {
                    order.setCall_str(sendResult.getMsg());
                    deptMapper.smsNum(map1);
                }
            } else {
                order.setCall_str("通知失败");
            }
            yp.close();
            tOrderService.updateById(order);
        }
//        [
//        {
//            "sid": 9527,
//                "uid": null,
//                "user_receive_time": "2014-03-17 22:55:21",
//                "error_msg": "DELIVRD",
//                "mobile": "15205201314",
//                "report_status": "SUCCESS"
//        },
//        {
//            "sid": 9528,
//                "uid": null,
//                "user_receive_time": "2014-03-17 22:55:23",
//                "error_msg": "DELIVRD",
//                "mobile": "15212341234",
//                "report_status": "SUCCESS"
//        }
//]

        System.out.print("------------------->>" + sms_status);
    }

    @RequestMapping(value = "/ordersNum")
    @ResponseBody
    public Object ordersNum(String query) {
        List<Map<String, Object>> selectOrders = null;

        if (!StringUtils.isEmpty(query)) {
            int dot_id = userMapper.selectById(ShiroKit.getUser().getId()).getDot_id();
            selectOrders = tOrderService.selectOrders(query, dot_id + "");
        }

        return selectOrders;
    }

    /**
     *自提批量核销
     */
    @RequestMapping("/selfLiftBatchCheck")
    @ResponseBody
    public Object selfLiftBatchCheck(String ids) {
        List<Integer> mIds=new ArrayList<>();
        List<Integer> tIds=new ArrayList<>();
        for(String id:ids.split(",")){
            TOrder order=new TOrder();
            order.setId(Integer.valueOf(id));
            order.setFlow_status(4);//0 开单 1 车次核销 2 出库 3 收银 4 核销
            EntityWrapper<TOrderFee> eWrapper = new EntityWrapper<TOrderFee>();
            eWrapper.where("order_id={0}", id + "");
            List<TOrderFee> orderFees = orderFeeWarpper.selectList(eWrapper);
            for (TOrderFee tOrderFee : orderFees) {
                tOrderFee.setAuditStatus(3);
//                itOrderFeeService.updateById(tOrderFee);
                tIds.add(tOrderFee.getId());
            }
            orderFeeWarpper.batchOrderFeeAuditStatus("3","",tIds);
//            orderMapper.updateById(order);
            mIds.add(Integer.valueOf(id));

        }
        orderMapper.batchOrderAuditStatus("4",mIds);
        return super.SUCCESS_TIP;
    }
    /**
     *   单笔出库自提单回库存
     */
    @RequestMapping("/revokeOrder")
    @ResponseBody
    public Object revokeOrder(String id) {
        tOrderService.revokeOrder(id);
        return super.SUCCESS_TIP;
    }
    /**
     * 自提单新增子单(配送单)
     */
    @RequestMapping("/addSubDelivery")
    @ResponseBody
    public Object addSubDelivery(Integer id,Double tff,Double dshk,Double ldf,Double  shf,Double byf,Double ccf) {
        String  original_num =getPara("original_num");
        Wrapper<TOrder> wrapper = new EntityWrapper<>();
        wrapper = wrapper.where("original_num={0}", original_num).and("dot_id",ShiroKit.getUser().getDotId());
        List<TOrder> orderList = tOrderService.selectList(wrapper);
        if(orderList.size()>0){
            return new ErrorTip(BizExceptionEnum.ORDER_NUM_EXIST.getCode(),BizExceptionEnum.ORDER_NUM_EXIST.getMessage());
        }
        tOrderService.addSubDelivery(id,tff,dshk,ldf,shf,byf,ccf,original_num);//id,提付，代收货款，落地费，送货费，搬运费，仓储费
        return super.SUCCESS_TIP;
    }

}
